0

ステップバイステップの説明を表示するサイトを作成しようとしています。ユーザーは質問を表示し、回答を選択します。回答ビューを以下に示します。

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

ユーザーが「回答」を選択すると、Step フォームをレンダリングする Step#new にリダイレクトします。

<%= 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 から隠しフィールドに渡します。

Steps has_many :questions, :through=>:instruction を考えると、Steps コントローラーが Step を作成した後に、隠しフィールドの値を Instructions モデルに挿入するにはどうすればよいですか?

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
4

1 に答える 1

1

モデルの関係を尋ねましたが、3 つのモデルが互いにどのように関連しているかはまだ明確ではありません (ステップ has_many :questions, :through=>:instruction のみを言及しました)。とにかく、私は私の仮定に基づいてあなたの質問に答えます。注意してください:
モデル:

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 の観点からも非常に紛らわしいもの
です。
別の方法で処理できない場合は、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

したがって、ステップ モデル:

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

私はあなたがアイデアを得ると思います。

于 2014-08-31T18:31:01.413 に答える