Rails如何在一个页面上显示现有的相关记录并允许新的相关记录输入

时间:2014-09-01 04:41:04

标签: ruby-on-rails forms has-many

我的主要记录是牌照(如牌照所示) 它可以有很多翻译(含义或评论) 在一页上我想:
1)显示板记录并允许用户对板进行评级 2)显示所有现有的翻译记录,并允许用户投票给一个 3)允许用户创建新的翻译记录
我正在使用form_for the Plate,以及fields_for用于现有的翻译记录 我的研究表明应该有办法做到这一点,但我的代码并没有提供一个空白表格。目前我正在尝试第二个fields_for循环 目前,我只专注于我的渲染表单页面,我还没有处理输入。 最终的fields_for用于显示新翻译输入的空白​​框,而是显示最后的现有翻译 我将不胜感激任何建议 以下是我的编辑表单:_rate_or_vote.html.erb

<!-- app/views/plates/_rate_or_vote.html.erb -->
<%= form_for(@plate) do |f| %>

<% if @plate.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@plate.errors.count, "error") %>
      prohibited this plate from being updated:</h2>
    <ul>
      <% @plate.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

<h1>Rate the Plate</h1>

<span class="form-group">
  <%= f.label :input_rating, "Your Plate Rating" %>
  <%= f.select :input_rating, options_for_select(RATINGS)  %>
</span>&nbsp&nbsp
<span class="form-group">
  <%= f.label :rating, "Average Rating" %>
  <%= f.text_field :rating , :value => (number_with_precision(f.object.rating, :precision => 2) || 0) %>
</span>&nbsp&nbsp
<span class="form-group">
  <%= f.label :plate_number, "Plate Number" %>
  <%= f.text_field :plate_number %>
</span>&nbsp&nbsp
<span class="form-group">
  <%= f.label :state, "State" %>
  <%= f.text_field :state, class: "form-control" %>
</span><br><br>
<span style="background-color:#DCDCDC; color:#000000; font-style: normal; font-family:Georgia;">
  Contributor</span>
<span style="border:1px solid black;padding:3pt;">
  <%= @plate.user.full_name %></span>&nbsp&nbsp
<span class="form-group">
  <%= f.label :plate_image, "Plate Image File" %>
  <%= f.file_field :plate_image %>
</span><br><br>

<h1>Existing Translations and/or Comments - You can vote for one</h1>
<!-- %= f.fields_for :translations do |ff| %-->
<%= f.fields_for :translations, @plate.translations do |ff| %>
<span>Click this box to vote for this translation:</span>&nbsp
<span><%= check_box_tag "translation_ids[]", :id %></span>&nbsp&nbsp
<span><%= ff.label "Current votes" %>
  <%= ff.text_field :votes, :value => (number_to_percentage(ff.object.votes / ( @plate.translation_votes.nonzero? || 1 ) * 100,
  precision: 0)) %>
</span>&nbsp&nbsp
<span style="background-color:#DCDCDC; color:#000000; font-style: normal; font-family:Georgia;">
  Contributor</span>
<span style="border:1px solid black;padding:3pt;">
  <%= ff.object.user.full_name %></span><br><br>
<span>Translation and/or Comment</span><br>
<span>
<%= ff.text_area :meaning, :cols => 60, :rows => 10  %>
</span><br><br>
<% end %>

<h1>Offer a new Translation and/or Comment</h1>
<!-- %= f.fields_for :translations do |ff| %-->
<%= f.fields_for :translations, @plate.translations do |fff| %>
  <div>
<!-- %= fff.label :meaning, "Translation" %-->
    <%= fff.text_field :meaning %>
  </div><br><br>
<% end %>

<%= f.submit "Save and/or Return", :name => "update", class: "btn btn-default" %>

1 个答案:

答案 0 :(得分:0)

好的,所以@plate.translations是已经在板上的当前翻译集。

如果你想要一个新的字段用于...那你必须实际建立一个新的。

例如:

<%= f.fields_for :translations, [@plate.translations.build] do |fff| %>
  <div>
    <%= fff.text_field :meaning %>
  </div><br><br>
<% end %>