即使我定义了路线,也会出现“无路线匹配”错误

时间:2016-02-05 22:02:14

标签: ruby-on-rails ajax routes find

我正在使用Rails 4.2.3。我正在尝试编写一个AJAX方法来调用我的控制器中的方法,但是我得到了一个无路由器错误。在我的咖啡脚本中,我有这个

updateAll = (arg) ->
  object = $('#user_object_object').val()
  day = $('#user_object_day').val()
  $.ajax
    url: "/find_by_user_object_and_day"
    type: 'GET'
    data: {day: day, object: object}
    success: (data) ->
      $('total').val(data)
    error: ->
      alert "Something went wrong"

在我的config / routes.rb中我有这个

Rails.application.routes.draw do
  resources :user_objects do
    get :find_by_user_object_and_day, on: :collection 
  end

然后在我的“./app/controllers/user_objects_controller.rb”中我有这个

def find_by_user_object_and_day
  Userobject.find_by_user_object_and_day(:params[user], :params[object], :params[day])
end

但是当调用我的AJAX方法时,会在服务器上产生此错误......

F, [2016-02-05T15:52:29.916816 #12058] FATAL -- : 
ActionController::RoutingError (No route matches [GET] "/find_by_user_object_and_day"):
  actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.5) lib/rails/rack/logger.rb:22:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.5) lib/rails/engine.rb:518:in `call'
  railties (4.2.5) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
  /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
  /Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'

2 个答案:

答案 0 :(得分:0)

问题是您的路线中有nested resource,这意味着您将获得嵌套的网址/路径:

resources :user_objects do
    get :find_by_user_object_and_day, on: :collection #-> url.com/users_objects/:user_object_id/find_by_user_object_and_day
end

由于您必须在JS中明确定义路径(它无法知道您希望向访问者发送哪个:user_object_id),因此您必须将ajax#url值更改为以下内容:

updateAll = (arg) ->
  user_object_id = $('#user_object').data("id") #-> however you define the id
  object         = $('#user_object_object').val()
  day            = $('#user_object_day').val()
  $.ajax
    url: "user_objects/" +  user_object_id + "/find_by_user_object_and_day"

答案 1 :(得分:-1)

当您在资源'user_objects中定义路由时,路径将在/ user_objects / find_by_user_object_and_day,而不是/ find_by_user_object_and_day处可用。

如果您希望它能够在根级别/ find_by_user_object_and_day,那么您还需要在根级别定义路由,如下所示:

Rails.application.routes.draw do
  get '/find_by_user_object_and_day', to: 'user_objects#find_by_user_object_and_day'
end