在其他控制器中渲染嵌套表单部分

发布于 2024-11-25 06:36:08 字数 1357 浏览 3 评论 0原文

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

我已将所有表单嵌套在作业/新视图上。

现在该应用程序的目标是让管理员(我们)在管理墙后面创建工作和问题。完成此操作后,我们希望列出为每个特定作业创建的问题,并让用户回答问题。这需要将答案表单放在不在管理墙后面的另一个视图(相同或不同的控制器)上。

由于表单全部嵌套在作业/新视图上,因此我为答案表单创建了一个部分:

 <%= 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 %>

我的想法是我创建了一个部分,我将能够引用它我想要在应用程序中的任何位置,但我一直在尝试一切,但似乎无法让它工作。

我基本上有两个问题:

1)我如何在另一个视图上单独渲染部分答案(正确的代码是什么)? 2)创建此视图的最佳位置在哪里?我最初的想法是另一个作业视图,因为它将嵌套在父资源下,但我不完全确定这是否有效。

谢谢,如果您需要更多信息或说明,请告诉我。

I have three resources: Jobs, Questions and Answers.
The relationships are: Job has many questions; Question has many Answers; Job has many Answers.

I have nested all of the forms on the jobs/new view.

Now the goal of the app is for the admins (us) to create jobs and questions behind an admin wall. Once we do that we want to list out the questions created for each specific job and have the users answer the questions. This requires putting the answers form on another view (same or different controller) that is not behind an admin wall.

Since the forms are all nested on the jobs/new view, I created a partial for the answers form:

 <%= 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 %>

My thinking was that I create a partial and I will be able to reference it wherever I want in the app, but I have been trying everything and I can't seem to get it to work.

I basically have 2 questions out of this setup:

1) How do I go about rendering the answers partial by itself on another view (what is the correct code)?
2) Where is the best place to create this view? My initial thinking was another jobs view since it will be nested under the parent resource, but i'm not entirely sure if this works.

Thanks and let me know if you need any more info or clarification.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

仲春光 2024-12-02 06:36:08

尝试以下

   <%= render :partial=> '/partials/answer_fields', :f => builder %>

而不是

   <%= render 'partials/answer_fields', :f => builder %>

Try following

   <%= render :partial=> '/partials/answer_fields', :f => builder %>

Instead of

   <%= render 'partials/answer_fields', :f => builder %>
擦肩而过的背影 2024-12-02 06:36:08

如何保持与此处相同的结构,但不渲染与 Job 和 Question 模型关联的任何字段?这样,嵌套仍将传递到控制器,并且您不必更改该功能。

<%= form_for(@job) do |f| %>
  <%= f.fields_for :questions do |q| %>
    <%= q.fields_for :answers do |a| %>
      <%= render 'partials/answer_fields', :f => a %>
    <% end %>
  <% end %>
<% end %>

这对你有用吗?

How about keep the same structure you have here, but do not render out any fields associated with the Job and Question models? This way the nesting will still be passed to the controller and you don't have to change that functionality.

<%= form_for(@job) do |f| %>
  <%= f.fields_for :questions do |q| %>
    <%= q.fields_for :answers do |a| %>
      <%= render 'partials/answer_fields', :f => a %>
    <% end %>
  <% end %>
<% end %>

Would that work for you?

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