title: Create and edit object using modal in rails
body: <p>I've been trying to create and edit an object using a modal view in the index html in Rails. I'm using twitter bootstrap in the project. I m so far successful in creating the object using the modal. 
  For things to work, I had to create an object called @post = Post.new in my index action.</p>
  
  <p>Since before the edit modal is displayed, the edit object must be ready as @post = Post.find(params[:id]), but it happens in the edit action. </p>
  
  <p>Is their a way using which I can initialize @post for my edit modal view before it is displayed?</p>
  
  <p>Here's my code:</p>
  
  <p>index.html.erb</p>
  
  <pre><code>&lt;div class="page-header"&gt;
      &lt;h1&gt;Posts&lt;/h1&gt;
  &lt;/div&gt;
  
  
  &lt;% @posts do |post| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= post.name %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= post.description %&gt;&lt;/td&gt;
  
      &lt;td&gt;&lt;%= link_to 'Edit', edit_post_path(post), :class =&gt; 'btn btn-mini btn-min-standard-width',  :data =&gt; {:toggle =&gt; "modal", :target =&gt; "#editItemModal"} , :remote =&gt; true %&gt;
          &lt;%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete, :class =&gt; 'btn btn-mini btn-danger btn-min-standard-width'  %&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;% end %&gt;
  
  
  &lt;%= link_to 'New Item', new_post_path, 
          :class =&gt; 'btn btn-primary btn-standard-width' ,  :data =&gt; {:toggle =&gt; "modal", :target =&gt; "#newItemModal"} , :remote =&gt; true %&gt;
  
  
  
  
  
  &lt;div id="newItemModal" class="modal hide fade" &gt;
      &lt;button type="button" class="close" data-dismiss="modal"&gt;×&lt;/button&gt;&lt;/h1&gt;
      &lt;div class="page-header"&gt;
          &lt;h1&gt;New item &lt;/h1&gt;
      &lt;/div&gt;
  
      &lt;%= render :partial =&gt; 'form' %&gt;
  &lt;/div&gt;
  
  
  &lt;div id="editItemModal" class="modal hide fade" &gt;
      &lt;button type="button" class="close" data-dismiss="modal"&gt;×&lt;/button&gt;&lt;/h1&gt;
      &lt;div class="page-header"&gt;
          &lt;h1&gt;Edit item &lt;/h1&gt;
      &lt;/div&gt;
  
      &lt;%= render :partial =&gt; 'form' %&gt;
   &lt;/div&gt;
  </code></pre>
  
  <p>_form.html.erb</p>
  
  <pre><code>&lt;%if @post%&gt;
  
      &lt;%= form_for(@post, :html =&gt; { :class =&gt; 'form-horizontal', :remote =&gt; true } ) do |f| %&gt;
  
  
       &lt;div class="control-group"&gt;
          &lt;%= f.label :name, :class =&gt; 'control-label' %&gt;
          &lt;div class="controls"&gt;
             &lt;%= f.text_field :name %&gt;
         &lt;/div&gt;
       &lt;/div&gt;
      &lt;div class="control-group"&gt;
          &lt;%= f.label :description, :class =&gt; 'control-label' %&gt;
          &lt;div class="controls"&gt;
              &lt;%= f.text_area :description %&gt;
          &lt;/div&gt;
       &lt;/div&gt;
  
  
      &lt;div class="form-actions"&gt;
         &lt;%= f.submit nil, :class =&gt; 'btn btn-primary' %&gt;
       &lt;/div&gt;
  
  
      &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>PostController</p>
  
  <pre><code>class PostsController &lt; ApplicationController
  
   def index
      @post = Post.new
      @posts = Post.all
  
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @posts }
      end
    end
  
   def show
      @post = Post.find(params[:id])
  
      respond_to do |format|
         format.html # show.html.erb
         format.json { render json: @post }
       end
    end
  
   def new
      @post = Post.new
   end
  
    def edit
      @post = Post.find(params[:id])
      respond_to do |format|
        format.html
       format.json { render json: @post }
      end
    end
  </code></pre>
  
  <p>............</p>