Undefined method _s_path in form_for

2019-04-17 02:23发布

问题:

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?

回答1:

Since the route for workouts is nested under a user resource you want this format (IIRC) for the form_for:

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


回答2:

In my case, my problem was that my nested controller (ExpansionController) had a singular name and its parent (EditionsController) a plural name. I've changed my nested controller name to plural (ExpansionsController), its routes and it worked.