1

I know there are a bunch of questions on this. I've looked around, but I don't seem to be making the mistakes I've found answers to.

The form for my workout resource, nested under users, is giving the error

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

That form goes:

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

The Workouts Controller new action (where the error's being produced):

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

The workout model is singularly named. The controller is plural. Here's my routes file:

resources :users do 
  resources :workouts
end

And the relevant rake routes output:

       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

(Maybe it should be going to user_workouts_path, but I don't know why it wouldn't be doing that automagically.

Also, I'm using mongo/mongoid, but I don't know why that should make a difference. And I had my controller puts @workout, and it instantiates a new object that I can see in the server logs:

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>

Even tested that user_instance.workouts.new works in the console, to make sure it wasn't some weird mongo problem.

Ideas?

4

2 回答 2

6

由于锻炼路线嵌套在用户资源下,因此您希望以下格式 (IIRC) 用于form_for

form_for [@user, @workout] do |f|
于 2013-06-11T06:36:28.227 回答
0

就我而言,我的问题是我的嵌套控制器(ExpansionController)有一个单数名称,而它的父级(EditionsController)有一个复数名称。我已将嵌套控制器名称更改为复数(ExpansionsController),它的路由和它的工作。

于 2015-01-06T15:20:40.900 回答