form_for中未定义的方法_s_path [英] Undefined method _s_path in form_for

查看:69
本文介绍了form_for中未定义的方法_s_path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道对此有很多疑问.我环顾四周,但似乎并没有犯我找到答案的错误.

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>

该表格为:

= 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

以及相关的耙路输出:

       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,但是我不知道为什么它不会自动做到这一点.

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

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

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>

甚至测试过user_instance.workouts.new在控制台中是否可以正常工作,以确保它不是怪异的mongo问题.

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

想法?

推荐答案

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

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|

这篇关于form_for中未定义的方法_s_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆