ruby-on-rails - Ruby on Rails form_for 引用类 ID 并传递给子评论

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

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

我也做了一个小截图:http://prntscr.com/2pjk0i

问题 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

评论 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 %>

预先感谢您的帮助! :)

最佳答案

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

如果你不独立地与评论互动——如果总是有问题,那么把你的路线改成这样:

resources :questions do
  resources :comments
end

然后 - 在您的表单中,您将执行此操作

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

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

在评论 Controller 中 - 您将从 params[:question_id] 中获取问题并通过 ajax 响应(响应 json)返回结果。

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

关于ruby-on-rails - Ruby on Rails form_for 引用类 ID 并传递给子评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21563280/

相关文章:

ruby-on-rails - Heroku 可以配置为进行真正的无缝部署吗?

ruby-on-rails - Ruby Regex MatchData 对象捕获行为异常的方法

ruby - 无法在 Ruby-s 和 Go-s 实现之间创建相同的 scrypt 摘要

arrays - Ruby 合并排序——这个递归函数是如何工作的?

mysql - 使用关联创建新的 ruby​​ 模型实例

ruby-on-rails - 用于 Ruby/Rails 的基于组件的 UI 框架?

ruby-on-rails - heroku 和 gem 仅适用于开发环境

ruby-on-rails - 创建具有 has_one 关系的 FactoryBot 用户

ruby-on-rails - Controller 方法#show 被调用

ruby-on-rails - Rails 自定义元模型?