title: Rails, render edit form on click
body: <p>I have a list of all my "books" on the books.index view. I want to be able to edit them in the same view. Is there a way to render an edit form when I click on one of the Books ? </p>
  
  <p>As of right now, I'm printing out a partial for each book, and I've added an individual form for each book. But then I'd have to hide all of them and append them each time someone clicks on a book. </p>
  
  <p>I'm thinking there should be some kind of way for me to render the same form each time someone clicks on it, and give it a book id, then it will hopefully fill itself with the right information. </p>
  
  <p>Hope this is making sense.
  Thanks!</p>
  
  <p>Controller books: </p>
  
  <pre><code>class BooksController &lt; ApplicationController
  def index
      @Books = Book.all
      @book = Book.new
  
  end
  
  def show
      @book = Book.find(params[:id])
      @chapter = chapter.new
  end
  
  def new
      @book = Book.new
      if !current_user.admin?
          redirect_to @book
      end
  end
  
  def edit
      @book = Book.find(params[:id])
  
      if !current_user.admin?
          redirect_to @book
      end
  
  
  end
  
  def create
      @books = Book.all
      @book = Book.new(book_params)
  
      if @book.save
          respond_to do |format|
              format.js
              format.html { render action: "index", notice: 'Book was successfully created.' }
              format.json { render json: @book, status: :created, location: @book }
          end
      else
          respond_to do |format|
              format.js 
              format.json { render json: @book.errors, status: :unprocessable_entity }
              format.html { render action: "index", notice: 'feil' }
          end
      end
  
  
  
  end
  
  def update
      @book = Book.find(params[:id])
  
      if @book.update(book_params)
          redirect_to @book
      else
          render 'edit'
      end
  end
  
  def destroy
      @book = Book.find(params[:id])
      @book.destroy
  
      redirect_to books_path
  end
  
  private
      def book_params
      params.require(:book).permit(:title, :pages)
  end
  end
  </code></pre>
  
  <p>View, books.index</p>
  
  <pre><code>&lt;div id="wrapper"&gt;
      &lt;section id="page_wrap"&gt;
          &lt;h1&gt;books&lt;/h1&gt;
  
          &lt;table id="all_exps"&gt;
              &lt;tr&gt;
                  &lt;th&gt;Title&lt;/th&gt;
                  &lt;th&gt;abbr&lt;/th&gt;
                  &lt;th&gt;pages&lt;/th&gt;
                  &lt;th&gt;chapter&lt;/th&gt;
                  &lt;th class="edits"&gt;&lt;/th&gt;
                  &lt;%unless !session[:user_id]%&gt;
                      &lt;% if current_user.admin?  %&gt;
                          &lt;th class="edits"&gt;&lt;/th&gt;
                          &lt;th class="edits"&gt;&lt;/th&gt;
                      &lt;% end%&gt;
                  &lt;%end%&gt;
              &lt;/tr&gt;
  
  
              &lt;% @books.each do |book| %&gt;
                  &lt;%= render 'book', book: book %&gt;
              &lt;% end %&gt;
          &lt;/table&gt;
  
          &lt;%unless !session[:user_id]%&gt;
              &lt;% if current_user.admin? %&gt;
                  &lt;div class="makenew"&gt; 
                      &lt;a href="" class="add_new"&gt; Add Book&lt;/a&gt;
                  &lt;/div&gt;
  
                  &lt;div id="somedialog" class="dialog"&gt;
                      &lt;div class="dialog__overlay"&gt;&lt;/div&gt;
                      &lt;div class="dialog__content"&gt;
                          &lt;div class="close"&gt;&lt;%= image_tag('icons/delete2.png') %&gt;&lt;/div&gt;
                          &lt;h2&gt;Add Book&lt;/h2&gt;
                          &lt;%= render 'form' %&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;% end%&gt;
          &lt;% end%&gt;
      &lt;/section&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>View partial _book </p>
  
  <pre><code>&lt;tr&gt;
  
      &lt;td&gt;&lt;%= book.title %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= book.abbr %&gt;&lt;/td&gt;
      &lt;td&gt;
  
      &lt;/td&gt;
      &lt;td&gt;&lt;%= book.chapters.length %&gt;&lt;/td&gt;
  
      &lt;td class="edits show"&gt;&lt;%= link_to image_tag('icons/eye.png'), book_path(book) %&gt;&lt;/td&gt;
      &lt;%unless !session[:user_id]%&gt;
          &lt;% if current_user.admin?  %&gt;
  
              &lt;td class="edits"&gt;&lt;%= link_to image_tag('icons/edit3.png'), edit_book_path(book) %&gt;&lt;/td&gt;
              &lt;td class="edits"&gt;&lt;%= link_to image_tag('icons/delete2.png'), book_path(book),
              method: :delete,
              data: { confirm: 'Are you sure?' } %&gt;&lt;/td&gt;
          &lt;% end%&gt;
  
      &lt;%end%&gt;
  &lt;/tr&gt;
  
      &lt;%= form_for book do |f| %&gt;
  
          &lt;%= f.label :title %&gt;
          &lt;%= f.text_field :title %&gt;
  
          &lt;%= f.label :pages %&gt;
          &lt;%= f.text_field :pages %&gt;
  
  
          &lt;%= f.submit %&gt;
  
       &lt;% end %&gt;
  </code></pre>
  
  <p>Book model </p>
  
  <pre><code>class Book &lt; ActiveRecord::Base
      has_many :chapters, dependent: :destroy
  
      validates :title, presence: true,
          length: { minimum: 3 }
  end
  </code></pre>