带有嵌套资源的 Rails form_for 循环
我有三种资源:作业、问题和答案。
这些关系是: 工作有很多问题;问题有很多答案。
我在“作业”表单视图上创建了一个嵌套表单, 其中包括创造就业机会和问题。这些都将位于管理墙后面,但我希望用户通过答案表单视图上的表单来回答问题(而不是在墙后面)。
我面临的问题是我想为答案表单字段创建一个循环。
由于这是一个循环,并且会有超过 1 个答案字段,因此我希望问题能够动态呈现为答案表单标签。这将反映路径,因此jobs/1/questions/1/answers/new(编辑:这应该是jobs/1/answers/new
< /strong>)将显示 job_id
为 1 的所有问题。
我该如何执行此操作?我正在考虑在答案控制器中使用这样的新操作(我确信这是非常错误的):
def new
@answer = Answer.new
10.times do
@job = Job.find(params[:job_id])
@question = @job.questions.find(params[:question_id])
@answer = @question.answers.build(params[:answer])
end
end
这是我当前的答案表:
<%= form_for(@answer, :url => job_question_answers_path(@job, @question)) do |f| %>
<% f.fields_for :answers do |builder| %>
<%= builder.label @question.question %>
<%= builder.text_area :answer, :rows => 10 %>
<% end %>
<%= f.submit "Create" %>
<% end %>
如果您需要更多信息,请告诉我,并感谢您的帮助!
I have three resources: Jobs, Questions and Answers.
The relationships are: Job has many questions; Question has many Answers.
I have created a nested form on the Jobs form view,
which includes the creation of jobs and questions. Those are both going to be behind an admin wall, but I want the users to answer the questions through a form on the answers form view (not behind a wall).
The problem I am facing is that I want to create a loop for the answers form fields.
Since this is a loop and there will be more than 1 answer field, I want the questions to dynamically render as the answer form labels. This would be reflective of the path so jobs/1/questions/1/answers/new(EDIT: this should be jobs/1/answers/new
) would show all of the questions with a job_id
of 1.
How do I go about doing this? I was thinking of using a new action like this in the answers controller ( which i'm positive is very wrong):
def new
@answer = Answer.new
10.times do
@job = Job.find(params[:job_id])
@question = @job.questions.find(params[:question_id])
@answer = @question.answers.build(params[:answer])
end
end
And here is my current answers form:
<%= form_for(@answer, :url => job_question_answers_path(@job, @question)) do |f| %>
<% f.fields_for :answers do |builder| %>
<%= builder.label @question.question %>
<%= builder.text_area :answer, :rows => 10 %>
<% end %>
<%= f.submit "Create" %>
<% end %>
Let me know if you need any more info and thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我想出了如何让它工作,并使用railscasts剧集做到了:http://railscasts.com/episodes/196-nested-model-form-part-1。
我一开始就知道这个修复程序,但我没有使用它,因为我的应用程序的功能是允许管理员创建作业和问题(在墙后面),然后让用户回答这些相应的问题。我想,如果我将答案表单嵌套在工作和问题表单下,我将无法创建此管理员/用户功能(这是我的下一个壮举)。
我基本上只是将答案表格嵌套在工作/新行动的问题表格下。
它看起来像这样:
问题部分是:
答案部分是:
我必须在 Jobs#new 方法中进行更改:
最后,我在模型中的关联需要包括
像这样:
模型
此解决方案正在保存我的答案,但现在我需要在另一个视图中使用该部分来实现我尝试使用管理员/用户操作的功能。
如果您有任何疑问,请告诉我。
So I figured out how to get this working and I did it using the railscasts episode:http://railscasts.com/episodes/196-nested-model-form-part-1.
I was aware of this fix to begin with, but I held back from using it because the function of my app is to allow the admins to create jobs and questions (behind a wall) and then have the users answer those respective questions. I figured that if I nested the answers form under the job and questions form, I wouldn't be able to create this admin/user functionality (that is my next feat).
I basically just nested the answers form under the questions form on the job/new action.
It looks like this:
With the question partial being:
And the answers partial being:
I had to make a change in the Jobs#new method:
Lastly my associations in the models needed to include
Like so:
Models
This solution is saving my answers but now I need to use that partial in another view to fulfill the functionality that I was attempting with the admin/user actions.
Let me know if you have any questions.