title: Adding Answers to Questions in Ruby on Rails
body: <p>I am fairly new to Rails and I'm creating a question and answers site similar to stackoverflow. </p>
  
  <p>I have created the questions, but I'm not sure about how to now create the answers. </p>
  
  <p>I saw a similar post that had some info on so I tried </p>
  
  <pre><code>rails g resource Answer question_id:integer content:text user_id:integer
  </code></pre>
  
  <p>I have added in </p>
  
  <p>answer.rb</p>
  
  <pre><code>belongs_to :question
  belongs_to :user
  </code></pre>
  
  <p>question.rb
      belongs_to: user
      has_many:answers</p>
  
  <p>user.rb</p>
  
  <pre><code>has_many :answers
  has_many :questions
  </code></pre>
  
  <p>In my questions/Show, I have this:</p>
  
  <pre><code>&lt;div class="container"&gt;
  &lt;div class="row"&gt;
      &lt;div class="span12"&gt;
          &lt;h2&gt;&lt;%= @question.title %&gt;&lt;/h2&gt;
      &lt;/div&gt;
      &lt;div class="span6"&gt;
          Asked by &lt;%= link_to @question.user.full_name %&gt;        
    &lt;/div&gt;
      &lt;div class="span5 offset1"&gt;
          &lt;%= time_ago_in_words(@question.created_at) + " ago" %&gt;
        &lt;% if current_user.present? &amp;&amp; current_user.id == @question.user_id %&gt;
          &lt;%= link_to 'Edit', edit_question_path(@question) %&gt;
        &lt;% else %&gt;
  
        &lt;% end %&gt;
      &lt;/div&gt;
          &lt;hr&gt;
              &lt;p&gt;Description: &lt;%= @question.description %&gt;&lt;/p&gt;
        &lt;hr&gt;
  &lt;/div&gt;
  &lt;%= render 'answer' %&gt;  
  &lt;/div&gt;
  </code></pre>
  
  <p>In my questions/_answer.html.erb</p>
  
  <pre><code>&lt;div class="container"&gt;
  &lt;%= simple_form_for(@question.answer.new, html: {class: "form-horizontal"}) do |f| %&gt;
  &lt;% if @question.errors.any? %&gt;
    &lt;div id="error_explanation"&gt;
      &lt;h2&gt;&lt;%= pluralize(@question.errors.count, "error") %&gt; prohibited this question from being saved:&lt;/h2&gt;
  
      &lt;ul&gt;
      &lt;% @question.errors.full_messages.each do |msg| %&gt;
        &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
      &lt;% end %&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  
  &lt;%= f.input :content, :input_html =&gt; { :class =&gt; "span6", :rows =&gt; 4 }, label: 'Answer', placeholder: 'Type your answer here'%&gt;  %&gt;
  
  
  &lt;%= f.button :submit, :class =&gt; 'btn btn-inverse' %&gt;
  
  &lt;%= link_to (submit_tag 'Cancel', :type =&gt; :reset, :class =&gt; "btn btn-danger"), root_path %&gt;
  
  &lt;% end %&gt;
   &lt;/div&gt;
  </code></pre>
  
  <p>This is my questions_controller.rb</p>
  
  <pre><code>class QuestionsController &lt; ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update]
  before_action :set_question, only: [:show, :edit, :update, :destroy]
  
  # GET /questions
  # GET /questions.json
  
  
   def index
    @questions = Question.all
   end
  
  
  
   # GET /questions/1
    # GET /questions/1.json
    def show
    end
  
    # GET /questions/new
    def new
      @question = Question.new
    end
  
    # GET /questions/1/edit
    def edit
      @question = Question.find(params[:id])
  
      if @question.user == current_user
        render :edit
      else
        flash[:alert] = "You don't have permission to edit this question"
        redirect_to root_path
      end
    end
  
    # POST /questions
    # POST /questions.json
    def create
      @question = current_user.questions.new(question_params)
  
      respond_to do |format|
        if @question.save
          format.html { redirect_to @question, notice: 'Question was successfully created.' }
          format.json { render action: 'show', question: :created, location: @question }
        else
          format.html { render action: 'new' }
          format.json { render json: @question.errors, question: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /questions/1
    # PATCH/PUT /questions/1.json
    def update
      respond_to do |format|
        if @question.update(question_params)
          format.html { redirect_to @question, notice: 'Question was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render action: 'edit' }
          format.json { render json: @question.errors, question: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /questions/1
    # DELETE /questions/1.json
    def destroy
      @question.destroy
      respond_to do |format|
        format.html { redirect_to questions_url }
        format.json { head :no_content }
      end
    end
  
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_question
        @question = Question.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def question_params
        params.require(:question).permit(:title, :description)
      end
  end
  </code></pre>
  
  <p>But i'm not really sure what to write in my answers controller. </p>
  
  <p>I would really appreciate any tips!</p>
  
  <p>Thanks    </p>