【问题标题】:Route does not match in jquery with custom action路由在 jquery 中与自定义操作不匹配
【发布时间】:2015-07-15 01:39:10
【问题描述】:

我有名为 #load 的自定义控制器操作和名为 schedules_controller#save。我也有船模型,其中船has_many: schedules 和时间表belongs_to :boat

这里是routes.rb

resources :boats, except: :destroy do
      post '/schedules/load' => 'schedules#load'
      post '/schedules/save' => 'schedules#save'
  end

当我使用这条路线时,rake 给出了;

boat_schedules_load POST   /boats/:boat_id/schedules/load(.:format)    schedules#load
boat_schedules_save POST   /boats/:boat_id/schedules/save(.:format)    schedules#save

然后在我的Schedules_controller;

class SchedulesController < ApplicationController
before_filter :load_parent

def save 
    h = params[:event] 
    h.each do |key, value|
        @boat.schedules.create date: value["date"], notes: value["notes"],  price: value["price"], promo: value["promo"], status: value["status"]
    end
end

def load

end


def load_parent
     @boat = Boat.find(params[:boat_id])
end
end

我有一个模板,我在其中显示日程(日历)并有 Jquery,可能 Jquery 路径错误但我不知道如何在路径中使用 id;

overwiev.html.erb;

......
<div id="backend"></div> <!--This is for the calendar-->


<script>
$(document).ready(function() {
    $('#backend').DOPBackendBookingCalendarPRO({

        'DataURL': '/schedules/load', #THIS IS WRONG I SHOULD SMT LIKE PUT /boat_id/schedules/load
        'SaveURL': '/:boat_id/schedules/save'
    });
});
</script>

所以当我运行代码时,它给出了一个错误;

Routing Error

No route matches [POST] "/schedules/load"

我也有load.json.erb 文件,我想从数据库中加载数据,

<% schedule = @boat.schedules.all %>

<% if schedule.blank? %>

{"2010-01-08": {"available":"1",
                           "bind":0,
                           "info":"",
                           "notes":"",
                           "price":"20",
                           "promo":"",
                           "status":""
  }}

<% else %>

{"<%= schedule.last.date %>": {"available":"<%= schedule.last.available %>",
                           "bind":"<%= schedule.last.bind %>",
                           "info":"<%= schedule.last.info %>",
                           "notes":"<%= schedule.last.notes %>",
                           "price":"<%= schedule.last.price %>",
                           "promo":"<%= schedule.last.promo %>",
                           "status":"<%= schedule.last.status %>"
  }}


<% end %>

因为我在控制器上获得了船 ID,所以我认为 &lt;% schedule = @boat.schedules.all %&gt; 代码应该可以工作。但是,我尝试将路由(未嵌套)取出为;

resources :boats
post '/schedules/load' => 'schedules#load'
post '/schedules/save' => 'schedules#save'

然后报错;

ActiveRecord::RecordNotFound in SchedulesController#load

Couldn't find Boat with 'id'=

【问题讨论】:

    标签: jquery ruby-on-rails routes nested


    【解决方案1】:

    评论控制器中的过滤器:

    #before_filter :load_parent
    

    如果你想使用普通路由

    或者您需要更改脚本代码中的路径,例如:

    $(document).ready(function() {
     $('#backend').DOPBackendBookingCalendarPRO({
    
        'DataURL': '/boats/' + boat_id + '/schedules/load', #THIS IS WRONG I SHOULD SMT LIKE PUT /boat_id/schedules/load
        'SaveURL': '/boats/' + boat_id + '/schedules/load'
     });
    });
    </script>
    

    并且需要传递boat_id

    【讨论】:

    • 很抱歉再次打扰,但这一次它在 Jquery 上给出了错误。 'DataURL': '/boats/' + boat_id + '/schedules/load' 说没有定义boat_id
    • 当我尝试 'DataURL': '/boats/' + @boat.id + '/schedules/load' 时,由于它来自控制器,它给出了一个错误提示 Unexpected token : Illegal
    • 尝试像这样使用它:'DataURL': '/boats/' + + '/schedules/load'
    猜你喜欢
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    相关资源
    最近更新 更多