2011-10-07 4 views
0
<%= collection_select(:category, :category_id, Category.all, :id, :category_name ,:prompt=>"category") %> 

당신이 CATEGORY_ID이 볼 수있는 매개 변수에 불구하고 table.Even 거래에서 CATEGORY_ID를 삽입에 value_id를 제출 나던하지만 거래 데이터베이스에 전달되지 않습니다 code.Any 도움이 appreciated.Thank 될 것입니다 당신collection_select 데이터베이스 늘

<%= form_for @deal ,:url=>{:action =>"create"} do |c|%> 

    <%= c.text_field :item_name %><br/> 
    <%=c.fields_for :stores do |s| %> 
    <%=s.text_field :store_name %> 
    <%end%> 
    <%=c.fields_for :category do |d| %> 
    <%= collection_select(:category, :category_id, Category.all, :id, :category_name ,:prompt=>"category") %> 

    <%end%> 
    <%= c.submit "post"%> 

    <%end%> 

로그에서 모델

class Deal < ActiveRecord::Base 
belongs_to :category 
    accepts_nested_attributes_for :category 
    end 

    class Category < ActiveRecord::Base 
has_many :deals 
    end 

,
Parameters: {"utf8"=>"✓", "authenticity_token"=>"oO5AtFX4HUYAhcP15y/dFzn3kjVDmweykQPqgDDuupQ=", "deal"=> {"item_name"=>"grapes", "stores_attributes"=>{"0"=>{"store_name"=>"winco"}}}, "category_id"=>"2", "commit"=>"post"} 
    City Load (0.1ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = ? LIMIT 1 [["id", 2]] 
    SQL (1.5ms) INSERT INTO "deals" ("brand", "category_id", "city_id", "created_at", "item_name", "price", "size", "stars", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["brand", nil], ["category_id", nil], ["city_id", 2], ["created_at", Fri, 07 Oct 2011 23:16:45 UTC +00:00], ["item_name", "grapes"], ["price", nil], ["size", nil], ["stars", nil], ["updated_at", Fri, 07 Oct 2011 23:16:45 UTC +00:00]] 
    SQL (0.5ms) INSERT INTO "stores" ("address", "created_at", "store_name", "updated_at") VALUES (?, ?, ?, ?) [["address", nil], ["created_at", Fri, 07 Oct 2011 23:16:45 UTC +00:00], ["store_name", "winco"], ["updated_at", Fri, 07 Oct 2011 23:16:45 UTC +00:00]] 
    SQL (0.9ms) INSERT INTO "store_deals" ("address", "created_at", "deal_id", "store_id", "store_name", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["address", nil], ["created_at", Fri, 07 Oct 2011 23:16:45 UTC +00:00], ["deal_id", 4], ["store_id", 4], ["store_name", nil], ["updated_at", Fri, 07 Oct 2011 23:16:45 UTC +00:00]] 

답변

2

하여 사용해보십시오 :

<%= c.collection_select(:category_id, Category.all, :id, :name ,:prompt=>"category")%> 
or 
<%= c.collection_select(:Category_id, Category.all, :id, :name ,:prompt=>"category")%> 
or 
<%= c.select :category_id, Category.all.collect { |v| [ v.name, v.id ] } %> 

 <%=c.fields_for :category do |d| %> 

없이 거래는 카테고리에 포함되어있어 당신이 필요하지 않은 거래 모델의 CATEGORY_ID 또는 CATEGORY_ID 필드가됩니다

accepts_nested_attributes_for :category 
거래와 동시에 새 카테고리를 만들려고하지 않는 한

입니다.

+0

마침내 당신의 설명은 의미가있었습니다. 나는 정말로 들판을 가지고 있다고 생각하지 않았습니다. 감사합니다. – katie

1

params를 확인하십시오. category_id는 거래 해시 외부에있는 반면 (item_name 및 stores_attributes는 내부에 있음)

당신은 아마

<%= c.collection_select ... %> 선택한 카테고리 주위 fields_for을 제거하기 원하는 - 그것에 대한 이유가 없다. category_id는 카테고리가 아닌 거래의 속성입니다.

+0

<% = d.collection_select ... %> # 몇 가지 이유가 정의되지 않은 메서드'CATEGORY_ID '나에게 오류를 제공 <카테고리 : 0xa8a49b8> – katie

+0

CATEGORY_ID가 아닌 때문에 속성을 선택합니다. 왜 카테고리 선택에서 fields_for를 사용하고 있습니까? <% = c.collection_select : category, ... %> 잘 작동합니다. –

+0

나는 accepts_nested_attributes_for를 가지고 있기 때문에 field_for를 사용하고 있습니다 : 상점, : 거래 모델의 카테고리, 미안 해요. 내가 아직 업데이트하지 않았습니다. 여전히 도움을 제거한 후에도 마찬가지입니다. – katie

0

마지막이 하나

근무
<%= c.collection_select(:category_id, Category.all ,:id,:category_name,:prompt=>"category") %>