嵌套资源的形式
我已经浏览了大量的 form_for 嵌套资源问题,但无法找到任何适合我的解决方案。我想是时候问一个个性化的问题了。
我有两个模型,工作和问题,工作有很多问题和问题属于工作。
我使用脚手架创建控制器和模型,然后将资源嵌套在 paths.rb 中。
root :to => "pages#home"
resources :jobs do
resources :questions
end
get "pages/home"
get "pages/about"
get "pages/contact"
class Job < ActiveRecord::Base
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :job
end
现在我正在尝试访问“/jobs/1/questions/new”并不断收到
Questions#new 中的 NoMethodError
我从错误 No Route matches {:controller=>"questions"} 当代码是
<%= form_for(@question) do |f| %>
我知道这是错误的,所以我开始尝试其他组合,但没有一个起作用。
我已经尝试
<%= form_for([@job.questions.build ]) do |f| %>
过
<%= form_for([@job, @job.questions.build ]) do |f| %>
在
<%= form_for(@job, @question) do |f| %>
一堆其他组合中,但不起作用。
这是我的 rake 路线的链接: git clone https://gist.github.com/1032734
感谢任何帮助并让我知道您是否需要更多信息,谢谢。
I have gone through tons of the form_for nested resource questions and can't get any of the solutions to work for me. I figured its time to ask a personalized question.
I have two models, jobs and questions, jobs has_many questions and questions belong_to jobs.
I used scaffolding to create the controllers and models then nested the resources in the routes.rb.
root :to => "pages#home"
resources :jobs do
resources :questions
end
get "pages/home"
get "pages/about"
get "pages/contact"
class Job < ActiveRecord::Base
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :job
end
Right now I am trying to access '/jobs/1/questions/new' and keep getting the
NoMethodError in Questions#new
I started with the error No route matches {:controller=>"questions"} when the code was
<%= form_for(@question) do |f| %>
I know this is wrong, so I started to try other combos and none of them worked.
I've tried
<%= form_for([@job.questions.build ]) do |f| %>
that
<%= form_for([@job, @job.questions.build ]) do |f| %>
that
<%= form_for(@job, @question) do |f| %>
Among a bunch of other combinations and that are not working.
Here is a link to my rake routes : git clone https://gist.github.com/1032734
Any help is appreciated and let me know if you need more info, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是将 URL 作为额外选项传递:
编辑:
另请尝试:
I just pass the URL as an extra option:
EDIT:
Also try:
这就是我解决我的问题的方法:)
在您的
questions/_form.html.erb
中,为了使其正常工作,您需要作业的 id。您将按如下方式传递它:
在
questions_controller.rb
中,Build(
.build
) 与上面代码中使用 new(.new
) 类似,仅在旧版本中有所不同导轨版本;导轨 2 向下。现在进行创建操作(仍在
questions_controller.rb
中)如果您仅使用这些操作,则问题模型中的 job_id 和 user_id 字段将为空。要添加 id,请执行以下操作:
在您的
questions_controller.rb
中,将 job_id 添加到 job_params 中,如下所示:然后要传递用户的 id(如果您使用的是 Devise),请执行以下操作:
This is how I solved mine :)
In your
questions/_form.html.erb
For this to work, you need the job's id. You'll pass it as follows:
In the
questions_controller.rb
Build(
.build
) is similar to using new(.new
) in the code above, with differences only in older versions of rails; rails 2 down.Now for the create action (still in
questions_controller.rb
)If you only use these, the job_id and user_id field in the question model will be empty. To add the ids, do this:
In your
questions_controller.rb
add job_id to job_params like so:Then to pass the user's id (if you are using Devise), do: