Ruby on Rails form_for引用类id并传递给子注释

时间:2014-02-04 20:59:23

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

我有一个叫做“问题”的课程,类似于一篇文章,每个课程都有评论。现在的问题是,我想在索引页面上显示多个问题,并且所有问题都显示特定问题的评论以及一个小的形式来留下评论,这应该添加到它的问题中。基本上我添加了表单并完成了所有工作,除了弄清楚如何获取问题ID并将其传递给评论。

我也制作了一个小屏幕截图:http://prntscr.com/2pjk0i

questions_controller.rb

class QuestionsController < ApplicationController
before_action :set_question, only: [:show, :edit, :update, :destroy]

# GET /questions
# GET /questions.json
def index
  @current_user ||= User.find_by_id(session[:user_id])
  @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
end

# POST /questions
# POST /questions.json
def create
  @question = Question.new(question_params)
  @current_user ||= User.find_by_id(session[:user_id])

  @question.update(:user_id => @current_user.id)

  respond_to do |format|
    if @question.save
      format.html { redirect_to @question, notice: 'Question was successfully created.' }
      format.json { render action: 'show', status: :created, location: @question }
    else
      format.html { render action: 'new' }
      format.json { render json: @question.errors, status: :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, status: :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, :body)
  end
end

comments_controller.rb

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

  # GET /comments
  # GET /comments.json
  def index
    @current_user ||= User.find_by_id(session[:user_id])
    @comments = Comment.all
  end

  # GET /comments/1
  # GET /comments/1.json
  def show
  end

  # GET /comments/new
  def new
    @comment = Comment.new
  end

  # GET /comments/1/edit
  def edit
  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = Comment.new(comment_params)
    @current_user ||= User.find_by_id(session[:user_id])

    @comment.update(:user_id => @current_user.id, :question_id => ?) # What to add here to get the specific question id? 

    respond_to do |format|
      if @comment.save
        format.html { redirect_to '/', notice: 'comment was successfully created.' }
        format.json { render action: 'show', status: :created, location: @comment }
      else
        format.html { render action: 'new' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /comments/1
  # PATCH/PUT /comments/1.json
  def update
    respond_to do |format|
      if @comment.update(comment_params)
        format.html { redirect_to '', notice: 'comment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @comment.destroy
    respond_to do |format|
      format.html { redirect_to '' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:title, :body)
    end
end

index.html.erb

    <h1>Listing questions</h1>

<%= link_to 'New Question', new_question_path %>

<hr>

<% @questions.each do |question| %>

  <!-- Author -->
  <%= question.user.name %> <br>

  <!-- Date -->
  <%= question.created_at %> <br>

  <!-- Title -->
  <%= question.title %> <br>

  <!-- Body -->
  <%= question.body %> <br>

  <%= question.id %> <br>

  <!-- Comment count -->
  <%= question.comments.size %> Comment <br>

  <!-- Comments -->
  <% question.comments.each do |comment| %>

    <!-- Comment Author -->
    <%= comment.user.name %> <br>

    <!-- Comment Date -->
    <%= comment.created_at %> <br>

    <!-- Comment Body -->
    <%= comment.body %> <br>

  <% end %>

  <%= form_for(question.comments.new) do |f| %>
    <div class="field">
      <%= f.label :body %><br>
      <%= f.text_area :body %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
  <% end %>

  <hr>

<% end %>

提前感谢您的帮助! :)

2 个答案:

答案 0 :(得分:2)

form_for需要以某种方式提交question_id - 通过路线或通过表格。我推荐一条路线。

如果您不单独与评论互动 - 如果始终存在问题,请将您的路线更改为以下内容:

resources :questions do
  resources :comments
end

然后 - 在你的表格中,你会这样做

<%= form_for [question, question.comments.new] do |f| %>

这将导致表单提交(POST)到/ question /:question_id / comments,您可以从那里处理它。

在评论控制器中 - 你将从params [:question_id]中得到问题并通过ajax响应返回结果(回复json)。

如果您以前没有这样做,这部分仍然很棘手。如果您需要有关该部分的帮助,您可能会找到很好的例子或提出单独的问题......

答案 1 :(得分:1)

您可以在表单中添加隐藏字段

 <%= f.hidden_field :question_id, value: question.id %>

或者您可以更改表单

<%= form_for :comment, :url => comments_path(question_id:question.id) do |f| %>
    <div class="field">
      <%= f.label :body %><br>
      <%= f.text_area :body %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
  <% end %>

当你提交这个表格时,你会有像/ comments?question_id = id

这样的网址