Rails - 在另一个部分中渲染编辑表单部分
我有一张桌子,我正在尝试使其更加精简。在我看来(此视图已经是部分视图(_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 中的“@”去掉,但这也不起作用。
提前感谢您的帮助。
I have a table that I am trying to make a bit more streamlined. In my view (this view is already a partial (_recipe_ingredient.html.erb), I have the following code, where rendering the partial is not working:
<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>
Previously, I was using link_to to edit the recipe_ingredient as follows (which worked fine), but I would like not to have the user go to another page to edit - instead I want the form to be part of the table:
<td><%= link_to 'Edit Quantity', edit_recipe_ingredient_path(recipe_ingredient) %></td>
The edit partial (which the new non-working code calls) looks like:
<h1>Editing recipe_ingredient</h1>
<%= render 'recipe_ingredients/form', :recipe_ingredient => @recipe_ingredient %>
And the standard form partial looks like:
<%= 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 %>
Mainly, I'm confused why it works using link_to, but I can't simply render the partial. The error I'm getting is undefined method `model_name' for NilClass:Class
in the first line of the form partial.
I've tried taking the "@" off @recipe_ingredient in the form partial, but that doesn't work either.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在您的操作中使用 @recipe_ingredient 创建对象,我认为它是一个编辑操作。
希望这一切顺利。
just create object with @recipe_ingredient in your action i think its an edit action.
hope this will work fine.