2011-09-03 4 views
1

私はページ上に2つのcollection_selectヘルパーを使用しています。リスト自体は正しく設定されていますが、フォームを送信するとNULLが挿入されます。私がここで間違っていることを確かめない。 UPDATE:追加コントローラー・コードRuby for Rails collection_selectがモデルにフィールドを提出しない

New.html.erb:

<h1>New map_apps_suite</h1> 

<%= render 'form' %> 

<%= link_to 'Back', map_apps_suites_path %> 

フォームコード:

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

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

    <div> 
     <%= f.label "Application Name:" %> 
     <%= collection_select(:death_burrito_application, :id, DeathBurritoApplication.all, :id, :death_burrito_name, :prompt => true) %> 
    </div> 
     <br> 
     <br> 
    <div> 
     <%= f.label "Project Name:" %> 
     <%= collection_select(:custom_product_suite, :id, CustomProductSuite.all, :id, :product_suite_name, :prompt => true) %> 
    </div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

ログイン:

が127.0.0.1のために、 "/ map_apps_suites" POSTを開始at Fri Sep 02 20:57:10 -0700 2011 MapAppsSuitesControllerの処理#HTML形式で作成
のパラメータ:{} => "マップスイートのアプリ作成"、 "death_burrito_application" => { "ID" => "3200" を "コミット"、 "authenticity_token" => "0PP2U50CScjTbcUdRgbIjkExqo9k3psjlcf4w61ZpqI ="、 "UTF8" =>map_apps_suites [1m] [1m [35mSQL(13.0ms)[0m] [1m [36mSQL (0.0ms) [36mAREL(22.0ms)[0メートル[1mINSERT INTO map_apps_suitescustom_product_suite_iddeath_burrito_application_id)VALUES(NULL、NULL)[0メートル[1メートル[35mSQL (44.0ms)[0メートルを185ms

で発見302を完成 http://localhost:3000/map_apps_suites/3にリダイレクトCOMMIT

コントローラの作成のためのコード、新しい:

class MapAppsSuitesController < ApplicationController 
    before_filter :get_apps, :only => [:new, :edit, :destroy, :update] 
    before_filter :get_suites, :only => [:new, :edit, :destroy, :update] 

    def get_apps 
    @applications = DeathBurritoApplication.order(:death_burrito_name).all 
    end 

    def get_suites 
    @custom_prod_suites = CustomProductSuite.order(:product_suite_name).all 
    end 

    def new 
    @map_apps_suite = MapAppsSuite.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.xml { render :xml => @map_apps_suite } 
    end 
    end 
    def create 
    @map_apps_suite = MapAppsSuite.new(params[:map_apps_suite]) 
    Rails.logger.debug("Params: " + params.inspect) 

    respond_to do |format| 
     if @map_apps_suite.save 
     format.html { redirect_to(@map_apps_suite, :notice => 'Map apps suite was successfully created.') } 
     format.xml { render :xml => @map_apps_suite, :status => :created, :location => @map_apps_suite } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @map_apps_suite.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 
+0

を役に立てば幸いそれは

@map_apps_suite=MapAppsSuite.new(:custom_product_suite_id=>params[:custom_product_suite][:id], :death_burrito_application_id => params[:death_burrito_application_id][:id]) 

であるとして、次のようなメソッドを作成して、残っているHTMLを変更]

<%= f.collection_select(:death_burrito_application_id, DeathBurritoApplication.all, :id, :death_burrito_name, :prompt => true) %> <%= f.collection_select(:custom_product_suite_id, CustomProductSuite.all, :id, :product_suite_name, :prompt => true) %> 

2をcollection_selectするf.を追加答える。 – Salil

+0

コントローラーコードが追加されました。 – ScottJShea

答えて

1

私はあなたがあなたのcustom_product_suite_idに保存したいと思うdeath_burrito_application_idあなたmap_apps_suites table

にあなたは2つの方法

1を次のいずれかを使用して、これを保存することができますhtmlを変更してそのままコントローラコードを残す

<%= collection_select(:map_apps_suite, :death_burrito_application_id, DeathBurritoApplication.all, :id, :death_burrito_name, :prompt => true) %> 


<%= collection_select(:map_apps_suite, :custom_product_suite_id, CustomProductSuite.all, :id, :product_suite_name, :prompt => true) %> 

ORちょうど1ができるように、コントローラ側のコードを入力してください、私はこれが

+0

ありがとう! #1は働いていましたが、今は私の考え方が間違っています。私はf.collection_selectを試して、マージエラーが発生したので、間違ったツリーを吠えていると思った。 – ScottJShea

関連する問題