2012-03-07 4 views
0

わかりましたので、私の団体は、次のとおりです。Railsの3.2 - ネストされたリソースを渡すID

Outlet -> has_many :monitorings 
Monitoring -> belongs_to :outlet 

マイルート:

resources :outlets do 
    resources :monitorings 
end 

ビュー:

<%= link_to new_outlet_monitoring_path(@outlet) %> 

私は、リンクをクリックして、ログoutlet_idがパラメータとして新しいページに正しく渡されることを示します。 しかし、監視レコードを保存するとき、outlet_idはゼロになります。

助けが必要ですか?

UPDATE:

# views/monitorings/_form.html.erb 

<%= form_for(@monitoring) do |f| %> 
<h2>Type of Monitoring</h2> 
<fieldset data-role="controlgroup" > 
    <div class="radio-group"> 
     <%= f.radio_button :mtype, "Full" %><%= f.label :mtype, "Full", value: "Full" %> 
     <%= f.radio_button :mtype, "Partial" %><%= f.label :mtype, "Partial", value: "Partial" %> 
     <%= f.radio_button :mtype, "None" %><%= f.label :mtype, "None", value: "None" %> 
    </div> 
</fieldset> 
<hr> 
<%= f.submit "Next Step" %> 
<% end %> 

とコントローラ:

# controllers/monitoring_controller.rb 


def new 
    @monitoring = Monitoring.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @monitoring } 
    end 
end 

def create 
    @monitoring = Monitoring.new(params[:monitoring]) 

    respond_to do |format| 
    if @monitoring.save 
     format.html { redirect_to @monitoring, notice: 'Monitoring was successfully created.' } 
     format.json { render json: @monitoring, status: :created, location: @monitoring } 
    else 
     format.html { render action: "new" } 
     format.json { render json: @monitoring.errors, status: :unprocessable_entity } 
    end 
    end 
end 

答えて

1

これが最も可能性の高い、新しいモニター・レコードを作成する方法に問題があります。あなたのフォームとあなたのコントローラ作成アクションを見ることができますか?

+0

元の質問の更新版として追加しました:) – Ammar

+0

これを行うには2通りの方法があります。両方とも、新しいコントローラーアクションでコンセントオブジェクトを作成する必要があります。したがって、新しいアクションに '@outlet = Outlet.find(params [:outlet_id])'を追加してください。これで2つのオプションがあります。 1:@monitoringの宣言を@monitoring = @ outlet.monitorings.new'に変更し、あなたのビューでは何も変更しないでください。 2:@monitoringの宣言だけを残して、form_forを 'form_for [@outlet、@monitoring] do | f |'に変更してください。 – JohnColvin

+0

私はオプション2を使用しています。私は思っています**しかし、それは動作します。 – JohnColvin

関連する問題