在Ruby on Rails中添加问题的答案

时间:2013-11-28 12:25:58

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我是Rails的新手,我正在创建一个类似于stackoverflow的问题和答案网站。

我已经创建了问题,但我不确定如何创建答案。

我看到一篇类似的帖子有一些信息,所以我试过

rails g resource Answer question_id:integer content:text user_id:integer

我添加了

answer.rb

belongs_to :question
belongs_to :user

question.rb     belongs_to:用户     的has_many:答案

user.rb

has_many :answers
has_many :questions

在我的问题/节目中,我有这个:

<div class="container">
<div class="row">
    <div class="span12">
        <h2><%= @question.title %></h2>
    </div>
    <div class="span6">
        Asked by <%= link_to @question.user.full_name %>        
  </div>
    <div class="span5 offset1">
        <%= time_ago_in_words(@question.created_at) + " ago" %>
      <% if current_user.present? && current_user.id == @question.user_id %>
        <%= link_to 'Edit', edit_question_path(@question) %>
      <% else %>

      <% end %>
    </div>
        <hr>
            <p>Description: <%= @question.description %></p>
      <hr>
</div>
<%= render 'answer' %>  
</div>

在我的问题/ _answer.html.erb

<div class="container">
<%= simple_form_for(@question.answer.new, html: {class: "form-horizontal"}) do |f| %>
<% if @question.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>

    <ul>
    <% @question.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

<%= f.input :content, :input_html => { :class => "span6", :rows => 4 }, label: 'Answer', placeholder: 'Type your answer here'%>  %>


<%= f.button :submit, :class => 'btn btn-inverse' %>

<%= link_to (submit_tag 'Cancel', :type => :reset, :class => "btn btn-danger"), root_path %>

<% end %>
 </div>

这是我的questions_controller.rb

class QuestionsController < 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

但我不确定在我的答案控制器中写什么。

我真的很感激任何提示!

谢谢

1 个答案:

答案 0 :(得分:2)

也许尝试使用嵌套资源来创建问题的答案。与博客文章的评论相似。

http://railscasts.com/episodes/139-nested-resources