嵌套资源的表格 [英] form for nested resource
问题描述
我已经解决了很多有关嵌套资源的form_for问题,但找不到任何适合我的解决方案.我认为是时候问一个个性化的问题了.
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.
我用脚手架来创建控制器和模型,然后将资源嵌套在route.rb中.
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
现在我正在尝试访问'/jobs/1/questions/new'并继续获取
Right now I am trying to access '/jobs/1/questions/new' and keep getting the
问题#new
我从错误没有匹配代码{:controller =>"questions"} 的错误开始
<%= form_for(@question) do |f| %>
我知道这是错误的,所以我开始尝试其他连击,但没有一个起作用.
I know this is wrong, so I started to try other combos and none of them worked.
我尝试过
<%= form_for([@job.questions.build ]) do |f| %>
那个
<%= form_for([@job, @job.questions.build ]) do |f| %>
那个
<%= form_for(@job, @question) do |f| %>
在许多其他组合中,这些组合均无效.
Among a bunch of other combinations and that are not working.
这是我的耙路的链接:git clone https://gist.github.com/1032734
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.
推荐答案
我只是将URL作为附加选项传递:
I just pass the URL as an extra option:
<%= form_for(@question, :url => job_questions_path(@job)) do %>
也尝试:
form_for([@job, @question])
这篇关于嵌套资源的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!