2011-09-03 27 views
1

我在頁面上使用兩個collection_select助手。列表本身被正確填充,但是當我去提交表單時,NULL被傳遞給Insert。不知道我在這裏做錯了什麼。更新:新增控制器代碼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 %> 

登錄:

開始POST 「/ map_apps_suites」 爲127.0.0.1 at Fri Sep 02 20:57:10 -0700 2011 MapAppsSuitesController處理#以HTML創建
參數:{ 「提交」=> 「創建地圖應用套件」, 「death_burrito_application」=> { 「ID」=> 「3200」}, 「authenticity_token」=> 「0PP2U50CScjTbcUdRgbIjkExqo9k3psjlcf4w61ZpqI =」, 「UTF8」=> 「1mBEGIN [0m [1m [35mSQL(13.0ms)[0m描述爲 map_apps_suites [1m [1m [1mBEGIN [0m [1m [ [36mAREL(22.0ms)[0米[1mINSERT INTO map_apps_suitescustom_product_suite_iddeath_burrito_application_id)VALUES(NULL,NULL)[0米[1分[35mSQL (44.0ms)[0米COMMIT重定向到 http://localhost:3000/map_apps_suites/3完成302發現在185ms

控制器代碼創建新:

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

請提供控制器端的代碼,使一個人能夠回答。 – Salil

+0

只需添加控制器代碼。 – ScottJShea

回答

1

我想你想在你custom_product_suite_id保存和death_burrito_application_idmap_apps_suites table

你這個用以下兩種方式

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 Just添加f.到collection_select

<%= 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]改變創建方法像以下,並保持HTML,因爲它是

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

我希望這有助於

+0

謝謝! #1工作,現在我意識到我的思維錯誤。我曾嘗試過f.collection_select,並一直在收到合併錯誤,所以認爲我在吼叫錯誤的樹。 – ScottJShea

相關問題