ROR:使用partials

时间:2013-03-30 17:47:19

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

在我页面的左侧,有一个用户已创建的文档列表。以及创建/编辑文档的表单。下面是如何设计页面的。

| list of documents| Edit/Create documents|
|                  |                      |
|                  |                      |

我正在尝试执行以下操作

1)显示页面首次加载时创建新文档的表单。点击按钮点击重新加载包含更新列表和新创建文档的页面。

2)当用户点击其中一个现有文档时,我想更新表单以显示文档的详细信息。

我当时认为部分是这样做的。

我完成了(1)的一部分。现在我正在尝试加载现有文档的详细信息。

当我尝试显示现有文档的详细信息时,会显示此错误。

documents/_form.html.erb where line #1 raised:

undefined local variable or method `document' for #<#<Class:0x007ffe96ff30b0>:0x007ffe96f0e118>
Extracted source (around line #1):

1: <%= form_for document do |f| %>
2: <table>
3:      <tr>
4:      <div class="field">
Trace of template inclusion: app/views/documents/edit.html.erb

EDIT.html.erb

<h1>Editing document</h1>

<%= render 'form' %>

Index.html.erb

    <div id="docList">
    <%= search_field(:user, :document) %>

    <% @documents.each do |document| %>
            <li><%= link_to document.name, edit_document_path(document.id) %></li>
    <% end %>
    <br />

    <%= link_to 'New Document', new_document_path %>
    </div>
    <div id="docEdit">
            <%= render :partial => "form", :locals => { :document => Document.new } %>
    </div> 

_form.html.erb

<%= form_for document do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :notes %><br />
    <%= f.text_area :notes %>
  </div>
   <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

更新 ** ** documents_controller.rb

 #GET /documents/new
  # GET /documents/new.json
  def new
    @document = Document.new
    @document.user = current_user

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @document }
    end
  end

  # GET /documents/1/edit
  def edit
    @document = Document.find(params[:id])
  end

由于

1 个答案:

答案 0 :(得分:1)

试试这个:

<div id="docEdit">
    <%= render "Folder_name/form", document: Document.new  %>
</div> 

UPD:当您访问/ documents / 10 / edit时,编辑操作中的控制器找到id = 10的doc,并将var @document中的doc返回到edit.html.erb。在这个模板中,你调用render'form',在那里你将var @document(render'form',document:@document)传递给你的本地var文档。 只需添加您的edit.html.erb:

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