2016-08-11 83 views
0

我在商業應用程序中有一個表單,用戶可以在其中添加物品清單。引用屬性不保存到模型的新實例

在此創建#項目的形式,我希望用戶能夠(從下拉菜單)選擇他們的定價是在什麼貨幣。

(我創建了一個模型貨幣,視圖和控制器因此管理員可以添加貨幣類型(我想給管理員能夠限制貨幣類型))

下面是遷移文件:。

class CreateCurrencies < ActiveRecord::Migration 
    def change 
    create_table :currencies do |t| 
     t.string :name 
     t.string :symbol 

     t.timestamps null: false 
    end 
    end 
end 

class AddCurrencyToApartments < ActiveRecord::Migration 
    def change 
    add_column :apartments, :currency_id, :integer 
    add_index :apartments, :currency_id 
    end 
end 

我有belongs_to的/的has_many連接貨幣和項目在模型中的關係。然後,我在創建#項目表單中實施了一個下拉菜單,用戶可以在其中選擇貨幣。

<%= f.select(:currency, Currency.all.collect {|u| [ raw(u.symbol), u.id ] }) %> 

我的問題是,爲什麼不currency_id保存到項目模型的新實例?

它似乎在params哈希表來顯示:

{"utf8"=>"✓", "authenticity_token"=>"...w==", "item"=>{"Name"=>"Test Item", "from_date(1i)"=>"2016", "from_date(2i)"=>"8", "currency"=>"2", "price"=>"7"..... "description"=>""}, "commit"=>"Create Item"} 

但不實際工作保存在提交:

0.2ms) begin transaction SQL (1.0ms) INSERT INTO "items" ("name", "from_date", … "created_at", "updated_at") VALUES (?, ?, ?, ?, ?....) [["name", "Test Item"], ["from_date", "2016-08-11"], ["created_at", "2016-08-11 15:07:50.146172"], ["updated_at", "2016-08-11 15:07:50.146172"]] 

我也一直沒能成功添加currency_id從導軌控制檯到公寓。

在此先感謝!

+1

應它是「currency_id」=>「2」而不是「currency」=>「2」? –

+0

是的,沒錯。感謝您抓住我的錯誤! –

回答

0

更改下拉菜單<%= f.select(:currency_id,Currency.all.collect {| U | [原料(u.symbol),u.id]})%>

相關問題