jsp
jsp

Reputation: 2646

ROR: using partials

On the left hand side of my page there is a list of documents that the user has already created. And a form to create/edit documents. Following a representation of how the page is designed.

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

I am trying to do the following

1) Show form for creating a new document when the page first loads.And on button click reload the page with the updated list and the document newly created.

2) When user clicks on one of the existing documents I want to update the form to display the details of the document.

I was thinking that partials would be the way to do this.

I have part of (1) done. Now I 'm trying to load the details of the existing documents.

This error is shown when I try to display the details of an existing document.

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

Thanks

Upvotes: 0

Views: 66

Answers (1)

hellaxe
hellaxe

Reputation: 491

Try this:

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

UPD: When you visit /documents/10/edit, controller in edit action find doc with id=10, and return doc in var @document to edit.html.erb. In this template you make call of render 'form', where you pass into your local var document a var @document (render 'form', document: @document). Just add in your edit.html.erb:

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

Upvotes: 1

Related Questions