2013-03-30 91 views
0

在我的頁面的左側有一個用戶已經創建的文檔列表。並創建/編輯文檔的表單。在表示頁面的設計之後。ROR:使用部分

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

我嘗試做以下

1)顯示形式創建一個新文檔時對按鈕的頁面第一loads.And點擊與更新的列表,新創建的文件重新加載頁面。

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 %> 

UPDATE ** ** 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 

感謝

+0

請出示您的控制器代碼。 – sscirrus

+0

我已經添加了文檔控制器的代碼。 – jsp

回答

1

試試這個:

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

UPD:當你訪問/文檔/ 10 /編輯,在控制器編輯操作找到id爲10的文檔,並將var @document中的doc返回到edit.html.erb。在這個模板中,你可以調用render'form',在那裏你傳入你的本地var文件var @document(render'form',document:@document)。 只需添加在您的edit.html.erb:

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

我沒有問題顯示新文檔的窗體。當我嘗試顯示for for現有文檔時出現錯誤。 – jsp

+0

您可以顯示代碼,您可以在這裏爲現有文檔調用這個部分?如果您的edit.html.erb模板包含部分調用,請將文檔傳遞給他,如呈現「folder_name/form」,文檔:@document? – hellaxe

+0

我已經添加了該代碼。它是index.html.erb – jsp