【问题标题】:Adding Answers to Questions in Ruby on Rails在 Ruby on Rails 中添加问题的答案
【发布时间】:2013-11-28 20:31:18
【问题描述】:

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

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

我看到一个类似的帖子,其中包含一些信息,所以我尝试了

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

我已经加进去了

answer.rb

belongs_to :question
belongs_to :user

问题.rb 归属地:用户 has_many:答案

用户.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>

这是我的问题_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

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

如果有任何提示,我将不胜感激!

谢谢

【问题讨论】:

  • 请点一下勾好吗。

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


【解决方案1】:

也许可以尝试使用嵌套资源来创建问题的答案。类似于博客文章中的 cmets。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2010-12-18
    • 2010-12-18
    • 1970-01-01
    • 2023-04-07
    • 2016-02-23
    相关资源
    最近更新 更多