2014-08-28 58 views
0

我正在尝试创建一个会显示分步说明的网站。用户将查看问题并选择答案。答案视图如下所示:Rails:在创建后关联一个对象

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

当用户选择“答案”时,我重定向到步骤#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,然后进入一个隐藏字段的相关问题。

鉴于步骤has_many:questions,:through =>:instruction,在步骤控制器创建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 
+0

你是什么意思:“我如何将隐藏的字段值插入指令模型”? 你可以请张贴模型和视图吗?我想那肯定很清楚你正在寻找什么。 – 2014-08-28 06:13:24

+0

基本上,说明与步骤和问题有关。我在回答问题时创建第一步后创建指令。问题是我如何将问题编号从Step#new传递给Step#create? – jstein 2014-08-30 03:55:02

回答

1

虽然我问你为模型的关系仍然不清楚这三种模式是如何相互关联的(你只提到:步骤的has_many:问题,:通过=>:指令)。无论如何,我基于我的假设回答你的问题。所以,要小心:
该机型:

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#创建为什么要这么做创建的指令?
如果您无法以其他方式处理它,请将其放入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 

我觉得你的想法。

+0

谢谢!这很有意义(并且你的假设是正确的)。一旦我尝试了这一点,我会接受,如果它的工作。 – jstein 2014-08-31 23:21:16

+0

我得到一个NoMethod错误,除非我删除“私人”。这会危害我的网站吗?我在哪里分配question_id?如果我在Step#new中赋值,它不会保存,而如果我将其赋值Step#create,则会得到“无ID无法找到问题”。 – jstein 2014-09-01 21:45:04

+0

对不起,有一个错字:改变了私人回调方法的名称。根据question_id:它来自表格,您已经发布了一个新的步骤。此外,我坚信你有一个架构问题。为什么这个问题已经存在,而它的指示必须在创建一个步骤后创建?无论如何,我强烈建议再次考虑对象及其关系。如果你需要帮助,你也可以在freenode的#rubyonrails上见到我。 – 2014-09-03 05:40:27

相关问题