jquery Rails Cancancan Ajax自定义操作不工作

vs91vp4v  于 12个月前  发布在  jQuery
关注(0)|答案(1)|浏览(106)

我有一个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)


我没有看到控制台中的日志,好像它甚至没有开始执行控制器操作,我错过了什么?谢谢

kxeu7u2r

kxeu7u2r1#

这是呈现的HTML表单

<form id="my-form" data-turbolinks="false" action="/shoppings/search" accept-charset="UTF-8" data-remote="true" method="post"><input type="hidden" name="authenticity_token" value="b4W-ish4XpxX_b6BSpIzeQ_sRLurmXmfFAJISUwuasOMZLK7ATRNMKmU4H4smqg-1WAnfdvZr4JJc2EyI8uFaw" autocomplete="off">
    <div class="form-group">
        <input type="radio" value="1" name="period" id="period_1"> 
        <label class="label_index" for="period">Tutte</label>

        <input type="radio" value="2" name="period" id="period_2"> 
        <label class="label_index" for="period">Ultimi 7 giorni</label>

        <input type="radio" value="3" name="period" id="period_3"> 
        <label class="label_index" for="period">Ultimi 30 giorni</label>

        <input type="radio" value="4" name="period" id="period_4"> 
        <label class="label_index" for="period">Cerca per date</label>
    </div>
 </form>

字符串
在控制台中,我有错误
未捕获的语法错误:意外的标记“T”,“Turbolinks”.不是有效的JSON
在浏览器的网络选项卡中,答案是Turbolinks.clearCache() Turbolinks.visit("http://localhost:3000/", {"action":"replace"}),它不会呈现视图文件夹中的search.js.erb文件

相关问题