form_for中未定义的方法_s_path

时间:2013-06-11 06:33:08

标签: ruby-on-rails ruby-on-rails-3 routes mongoid

我知道这有很多问题。我环顾四周,但我似乎没有犯错,我找到了答案。

我的锻炼资源的表单嵌套在用户之下,正在提供错误

undefined method `workouts_path' for #<#<Class:0x007f88939c9e60>:0x007f8891527b20>

表格如下:

= form_for @workout do |f|
... = f.submit

锻炼控制器新动作(产生错误的地方):

  def new
    @user = User.find(params[:user_id])
    @workout = @user.workouts.new
  end

锻炼模型以其独特的名字命名。控制器是复数。这是我的路线文件:

resources :users do 
  resources :workouts
end

相关的佣金路线输出:

       user_workouts GET    /users/:user_id/workouts(.:format)          workouts#index
                     POST   /users/:user_id/workouts(.:format)          workouts#create
    new_user_workout GET    /users/:user_id/workouts/new(.:format)      workouts#new
   edit_user_workout GET    /users/:user_id/workouts/:id/edit(.:format) workouts#edit
        user_workout GET    /users/:user_id/workouts/:id(.:format)      workouts#show
                     PUT    /users/:user_id/workouts/:id(.:format)      workouts#update
                     DELETE /users/:user_id/workouts/:id(.:format)      workouts#destroy

(也许它应该转到user_workouts_path,但我不知道为什么它不会自动执行。

另外,我正在使用mongo / mongoid,但我不知道为什么会有所作为。我让我的控制器放置@workout,它实例化了一个我可以在服务器日志中看到的新对象:

ActionView::Template::Error (undefined method `workouts_path' for #<#<Class:0x007f8893d0a480>:0x007f8893d12cc0>):
    1: = form_for @workout do |f|
    2:  
    3:  = f.label :name, "Name (optional)"
    4:  = f.text_field :name
  app/views/workouts/_form.html.haml:1:in `_app_views_workouts__form_html_haml___787289985246055156_70112286316300'
  app/views/workouts/new.html.haml:3:in `_app_views_workouts_new_html_haml___1568138279872555052_70112285908620'


  Rendered /Users/username/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.5ms)
  Rendered /Users/username/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.9ms)
  Rendered /Users/username/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (12.4ms)
HERE GOES THE
#<Workout:0x007f8893da8ce8>

甚至测试过user_instance.workouts.new在控制台中工作,以确保它不是一些奇怪的mongo问题。

想法?

2 个答案:

答案 0 :(得分:6)

由于锻炼路线嵌套在用户资源下,您需要form_for的格式(IIRC):

form_for [@user, @workout] do |f|

答案 1 :(得分:0)

在我的情况下,我的问题是我的嵌套控制器( ExpansionController )具有单数名称,其父( EditionsController )具有复数名称。我已将嵌套控制器名称更改为复数( ExpansionsController ),其路由和工作正常。