Rails:使用相同的局部来创建和编辑嵌套项目 [英] Rails: Use same partial for creating and editing nested items

查看:90
本文介绍了Rails:使用相同的局部来创建和编辑嵌套项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了 Rails入门教程来建立一个带有注释的简单博客. /p>

我将其应用于自己的场景:带有历史记录项的历史记录.一切都差不多好了,直到我意识到我需要能够编辑这些历史记录项(有点像编辑评论).

我已经知道了,因此在局部上有一个"Edit Item"链接,用于显示历史记录项.似乎在历史记录项目控制器中单击了编辑"操作.但是我得到一个带有空白字段的表单,该表单的按钮上显示创建".

显示历史记录项目的部分中的链接:

<%= link_to 'Edit Item', edit_history_history_item_path(history_item.history, history_item) %>

历史记录项目控制器中的编辑操作:

def edit
  @history = History.find(params[:history_id])
  @history_item = HistoryItem.find(params[:id])
end

edit.html.rb页面中引用部分内容的部分:

<%= render 'form' %>

部分本身:

<%= form_for([@history, @history.history_items.build]) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  (blah blah lots more fields)
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我注意到.build位于部分顶部"@ history.history_items"的末尾.我认为这是制作引用原始历史记录(或博客文章)的新历史记录项目(或博客文章的新评论)所必需的.当它是新的历史记录项时,有什么方法可以保留该部分,但是当我要编辑现有的历史记录项时,可以采用另一种方法吗?

解决方案

您只需要对Partial进行少量更改(请参见下文).您应该显式传递history_item,以便不必依赖于部分实例可使用的实例变量:

<%= render 'form', history_item: @history_item %>

然后:

<%= form_for([history_item.history, history_item]) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  (blah blah lots more fields)
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I followed the Getting Started With Rails tutorial to set up a simple blog with comments.

I went to apply it to my own scenario: histories with history items. Everything was more or less fine until I realized that I needed to have the ability to edit these history items (kind of like editing comments).

I've got it so there's an "Edit Item" link on the partial that displays the history items. It seems to hit the edit action in the history items controller. But I get a form with blank fields that says "Create" on the button.

Link from the partial that shows the history items:

<%= link_to 'Edit Item', edit_history_history_item_path(history_item.history, history_item) %>

The edit action in the history items controller:

def edit
  @history = History.find(params[:history_id])
  @history_item = HistoryItem.find(params[:id])
end

The part of the edit.html.rb page that references the partial:

<%= render 'form' %>

The partial itself:

<%= form_for([@history, @history.history_items.build]) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  (blah blah lots more fields)
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I've noticed the .build on the end of "@history.history_items" at the top of the partial. I assume this was required to make a new history item (or a new comment for a blog post) that references the originating history (or blog post). Is there some way I can keep that part for when it's a new history item, but do it another way when I want to edit an existing one?

解决方案

You just need to make a small change to the partial (see below). You should pass in the history_item explicitly so that you don't have to depend on what instance variables are available to the partial:

<%= render 'form', history_item: @history_item %>

then:

<%= form_for([history_item.history, history_item]) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  (blah blah lots more fields)
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

这篇关于Rails:使用相同的局部来创建和编辑嵌套项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆