带有嵌套资源的 Rails form_for 循环

发布于 2024-11-19 17:20:07 字数 1081 浏览 5 评论 0原文

我有三种资源:作业、问题和答案。
这些关系是: 工作有很多问题;问题有很多答案。

我在“作业”表单视图上创建了一个嵌套表单, 其中包括创造就业机会和问题。这些都将位于管理墙后面,但我希望用户通过答案表单视图上的表单来回答问题(而不是在墙后面)。

我面临的问题是我想为答案表单字段创建一个循环。

由于这是一个循环,并且会有超过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

快乐很简单 2024-11-26 17:20:07

所以我想出了如何让它工作,并使用railscasts剧集做到了:http://railscasts.com/episodes/196-nested-model-form-part-1

我一开始就知道这个修复程序,但我没有使用它,因为我的应用程序的功能是允许管理员创建作业和问题(在墙后面),然后让用户回答这些相应的问题。我想,如果我将答案表单嵌套在工作和问题表单下,我将无法创建此管理员/用户功能(这是我的下一个壮举)。

我基本上只是将答案表格嵌套在工作/新行动的问题表格下。
它看起来像这样:

<%= form_for(@job) do |f| %>
 <%= f.label :name %><br />
 <%= f.text_field :name %>
  <%= f.fields_for :questions do |builder| %>
   <%= render 'question_fields', :f => builder %>
  <% end %>
 <%= f.submit %>
<% end %>

问题部分是:

<%= f.label :question, "Question" %>
 <%= f.text_area :question, :rows => 10 %>
 <%= f.check_box :_destroy %>
 <%= f.label :_destroy, "Remove Question" %>

  <%= f.fields_for :answers do |builder| %>
   <%= render 'partials/answer_fields', :f => builder %>
  <% end %>

答案部分是:

 <%= f.label "Answer" %>
 <%= f.text_area :answer, :rows => 10 %>
 <%= f.hidden_field :question_id, :value => @question %>
 <%= f.hidden_field :job_id, :value => @job.id %>

我必须在 Jobs#new 方法中进行更改:

def new
 @job = Job.new
 10.times do
   question = @job.questions.build 
   1.times { question.answers.build }
 end
respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @job }
end
end

最后,我在模型中的关联需要包括

accepts_nested_attributes_for

像这样:

模型

class Job < ActiveRecord::Base
 has_many :questions
 has_many :answers

 accepts_nested_attributes_for :questions, :allow_destroy => true
 accepts_nested_attributes_for :answers, :allow_destroy => true
end

class Question < ActiveRecord::Base
  belongs_to :job
  has_many :answers

  accepts_nested_attributes_for :answers 
end

class Answer < ActiveRecord::Base
 belongs_to :job
 belongs_to :question
end

此解决方案正在保存我的答案,但现在我需要在另一个视图中使用该部分来实现我尝试使用管理员/用户操作的功能。

如果您有任何疑问,请告诉我。

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:

<%= form_for(@job) do |f| %>
 <%= f.label :name %><br />
 <%= f.text_field :name %>
  <%= f.fields_for :questions do |builder| %>
   <%= render 'question_fields', :f => builder %>
  <% end %>
 <%= f.submit %>
<% end %>

With the question partial being:

<%= f.label :question, "Question" %>
 <%= f.text_area :question, :rows => 10 %>
 <%= f.check_box :_destroy %>
 <%= f.label :_destroy, "Remove Question" %>

  <%= f.fields_for :answers do |builder| %>
   <%= render 'partials/answer_fields', :f => builder %>
  <% end %>

And the answers partial being:

 <%= f.label "Answer" %>
 <%= f.text_area :answer, :rows => 10 %>
 <%= f.hidden_field :question_id, :value => @question %>
 <%= f.hidden_field :job_id, :value => @job.id %>

I had to make a change in the Jobs#new method:

def new
 @job = Job.new
 10.times do
   question = @job.questions.build 
   1.times { question.answers.build }
 end
respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @job }
end
end

Lastly my associations in the models needed to include

accepts_nested_attributes_for

Like so:

Models

class Job < ActiveRecord::Base
 has_many :questions
 has_many :answers

 accepts_nested_attributes_for :questions, :allow_destroy => true
 accepts_nested_attributes_for :answers, :allow_destroy => true
end

class Question < ActiveRecord::Base
  belongs_to :job
  has_many :answers

  accepts_nested_attributes_for :answers 
end

class Answer < ActiveRecord::Base
 belongs_to :job
 belongs_to :question
end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文