Rails:创建后关联对象

时间:2014-08-28 05:02:38

标签: ruby-on-rails activerecord hidden-field after-create

我正在尝试创建一个将逐步显示的网站。用户将查看问题并选择答案。答案视图如下所示:

<p id="notice"><%= notice %></p>

<p>
  <strong>Post:</strong>
  <%= @question.post %>
</p>

<%= link_to 'Answer', new_step_path(:question_id=>@question.id) %> |
<%= link_to 'Edit', edit_question_path(@question) %> |
<%= link_to 'Back', questions_path %>

当用户选择&#34;回答&#34;时,我重定向到Step#new,这将呈现Step表单。

<%= form_for(@step) do |f| %>
  <% if @step.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@step.errors.count, "error") %> prohibited this step from being saved:</h2>

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

  <div class="actions">
    <%= @question.id %>
    <%= f.hidden_field :question_id, :value => @question.id %>
  </div>

  <div class="field">
    <%= f.label :post %><br>
    <%= f.text_field :post %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我从URL传递相关问题,然后进入隐藏字段。

鉴于步骤has_many:questions,:through =&gt;:指令,如何在Steps控制器创建步骤后将隐藏字段值插入到说明模型中?

class StepsController < ApplicationController
  before_action :set_step, only: [:show, :edit, :update, :destroy]

  # GET /steps
  # GET /steps.json
  def index
    @steps = Step.all
  end

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

  # GET /steps/new
  def new
    @step = Step.new
    @question = Question.find(params[:question_id])
  end

  # GET /steps/1/edit
  def edit
  end

  # POST /steps
  # POST /steps.json
  def create
    @step = Step.new(step_params)

    respond_to do |format|
      if @step.save
        @instruction = Instruction.create(:question_id=>@question, :step_id=>@step, :order=>1)

        format.html { redirect_to @step, notice: 'Step was successfully created.' }
        format.json { render action: 'show', status: :created, location: @step }
      else
        format.html { render action: 'new' }
        format.json { render json: @step.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /steps/1
  # DELETE /steps/1.json
  def destroy
    @step.destroy
    respond_to do |format|
      format.html { redirect_to steps_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def step_params
      params.require(:step).permit(:post)
    end
end

1 个答案:

答案 0 :(得分:1)

虽然我问你关于模型的关系,但是仍然不清楚这三个模型是如何相互关联的(你只提到过:步骤has_many:questions,:through =&gt;:指令)。无论如何,我根据我的假设回答你的问题。所以要小心:
模特:

class Step < ActiveRecord::Base
  belongs_to :instruction
  has_many :questions, 
    through: :instruction
end

class Instruction < ActiveRecord::Base
  has_many :steps
  has_many :questions
end

class Question < ActiveRecord::Base
  belongs_to :instruction
end

现在你的steps_controller.rb:
首先:代码中实例化@question在哪里?

@instruction = Instruction.create(:question_id=>@question, :step_id=>@step, :order=>1)

从REST的角度来看,这条线也很混乱:
为什么StepsController #create会创建指令? 如果您无法以其他方式处理它,请将其置于Step模型回调中。从交易的角度来看,你也会想要它;)
这就是为什么你的行动应该更像:

def create
  @step = Step.new(step_params)
  respond_to do |format|
    if @step.save
      format.html { redirect_to @step, notice: 'Step was successfully created.' }
      format.json { render action: 'show', status: :created, location: @step }
    else
      format.html { render action: 'new' }
      format.json { render json: @step.errors, status: :unprocessable_entity }
    end
  end
end

因此Step模型:

class Step < ActiveRecord::Base
  belongs_to :instruction
  has_many :questions, 
    through: :instruction
  attr_accessor :question_id
  before_create :create_related_instruction

  private
  def create_related_instruction
    self.create_instruction question_id: question_id, order: 1
  end
end

我想你明白了。