我有一个Rails应用程序,我在其中添加了Devise和Cancancan来进行身份验证和授权。在此之前,actio工作正常,现在在控制台浏览器中我看到重定向到主页。
我用JavaScript调用提交表单的动作
$("input[name='period']").click(function(){
if ($(this).val() == '1' || $(this).val() == '2' || $(this).val() == '3') {
$('#form_search_dates').hide();
var elem = $('#my-form')[0];
$(elem).submit();
}
else {
$('#form_search_dates').show();
}
});
字符串
这是我的控制器动作
class ShoppingsController < ApplicationController
before_action :authenticate_user!
load_and_authorize_resource
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, :alert => exception.message
end
def search
@shoppings = nil
logger.debug "Dentro Search"
@period = params[:period]
if @period.present?
logger.debug "Period: #{@period}"
if @period == '1'
logger.debug "Tutte"
@shoppings = Shopping.where(:user_id => current_user.id).order(date_shopping: :asc)
elsif @period == '2'
logger.debug "Ultimi 7 giorni"
@date_end = Date.today
@date_start = @date_end - 7
@shoppings = Shopping.where(:user_id => current_user.id).where(:date_shopping => @date_start..@date_end).order(date_shopping: :asc)
elsif @period == '3'
logger.debug "Ultimi 30 giorni"
@date_end = Date.today
@date_start = @date_end - 30
@shoppings = Shopping.where(:user_id => current_user.id).where(:date_shopping => @date_start..@date_end).order(date_shopping: :asc)
end
else
logger.debug "No parametri"
@shoppings = Shopping.where(:user_id => current_user.id).order(date_shopping: :asc)
end
@result_total_price = @shoppings.sum(:total_price)
respond_to do |format|
format.html
format.js
end
end
型
这就是我的能力rb
def initialize(user)
can :create, Shopping
can :read, Shopping, user_id: user.id
can :update, Shopping, user_id: user.id
can :destroy, Shopping, user_id: user.id
型
当我调用单击单选按钮的操作时,它会重定向到根URL,在控制台中我看到了这一点
Started POST "/shoppings/search" for ::1 at 2023-12-19 12:18:00 +0100
Processing by ShoppingsController#search as JS
Parameters: {"authenticity_token"=>"[FILTERED]", "period"=>"2"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Redirected to http://localhost:3000/
Completed 200 OK in 17ms (ActiveRecord: 0.5ms | Allocations: 2903)
型
我没有看到控制台中的日志,好像它甚至没有开始执行控制器操作,我错过了什么?谢谢
1条答案
按热度按时间kxeu7u2r1#
这是呈现的HTML表单
字符串
在控制台中,我有错误
未捕获的语法错误:意外的标记“T”,“Turbolinks”.不是有效的JSON
在浏览器的网络选项卡中,答案是
Turbolinks.clearCache() Turbolinks.visit("http://localhost:3000/", {"action":"replace"})
,它不会呈现视图文件夹中的search.js.erb文件