【问题标题】:Rails - Rendering an edit form partial within another partialRails - 在另一个部分中呈现部分编辑表单
【发布时间】:2011-09-27 01:35:34
【问题描述】:

我有一张桌子,我正在尝试使其更加精简。在我看来(这个视图已经是部分(_recipe_ingredient.html.erb),我有以下代码,其中渲染部分不起作用:

<tr>
  <td><%= recipe_ingredient.ingredient.name %></td>
  <td><%= recipe_ingredient.ingredient.weight1*recipe_ingredient.quantity %></td>
  <td><%= recipe_ingredient.ingredient.description1 %></td>
  <td><%= recipe_ingredient.quantity %></td>
  <td><%= render(:partial => 'recipe_ingredients/edit', :locals =>   
    {:recipe_ingredient=> recipe_ingredient}) %></td>
  <td><%= link_to 'Remove', recipe_ingredient, :confirm => 'Remove ingredient from 
    recipe?', :method => :delete %></td>  
</tr>

以前,我使用 link_to 来编辑 recipe_ingredient,如下所示(效果很好),但我不希望用户转到另一个页面进行编辑 - 相反,我希望表单成为表格的一部分:

<td><%= link_to 'Edit Quantity', edit_recipe_ingredient_path(recipe_ingredient) %></td>

编辑部分(新的非工作代码调用)如下所示:

<h1>Editing recipe_ingredient</h1>
<%= render 'recipe_ingredients/form', :recipe_ingredient => @recipe_ingredient %>

标准形式的部分看起来像:

<%= form_for(@recipe_ingredient) do |recipe_ingredient| %>
  <% if @recipe_ingredient.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@recipe_ingredient.errors.count, "error") %> prohibited this  
        recipe_ingredient from being saved:</h2>

      <ul>
      <% @recipe_ingredient.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

    <div class="field">
    <%= recipe_ingredient.label :quantity %><br />
    <%= recipe_ingredient.number_field :quantity %>
  </div>
  <div class="actions">
    <%= recipe_ingredient.submit %>
  </div>
<% end %>

主要是,我很困惑为什么它可以使用 link_to,但我不能简单地渲染部分。我得到的错误是表单部分第一行中的undefined method `model_name' for NilClass:Class

我尝试在部分表单中将“@”从@recipe_ingredient 中删除,但这也不起作用。

提前感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails-3 partial-views renderpartial


    【解决方案1】:

    只需在您的操作中使用@recipe_ingredient 创建对象,我认为这是一个编辑操作。

    @recipe_ingredient = RecipeIngredient.find_by_id(params[:id)
    

    希望这会正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多