2014-08-28 39 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

相關問題