2013-11-28 2 views
0

Я довольно новичок в Rails, и я создаю сайт вопросов и ответов, похожий на stackoverflow.Добавление ответов на вопросы в Ruby on Rails

Я создал вопросы, но я не уверен, как теперь создавать ответы.

Я видел подобный пост, который имел некоторую информацию о поэтому я попытался

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 

Но я не совсем уверен, что писать мой контроллер ответов.

Я бы очень признателен за любые советы!

Thanks

+0

Вы можете нажать на галочку, пожалуйста. –

ответ

Смежные вопросы