There are a lot of similar topics, but it doesn't help me. There is a Account model
class Account < ActiveRecord::Base
belongs_to :user
belongs_to :currency
attr_accessible :currency
accepts_nested_attributes_for :currency
end
I added attr_accessible and accepts_nested_attributes_for , but actually i don't know they needed or not. Another model Currency that that has 3 items - USD, EUR, RUR
class Currency < ActiveRecord::Base
has_many :accounts
attr_accessible :id
accepts_nested_attributes_for :accounts
end
So in account form I have a selectbox with currency:
<%= form_for @account do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :currency %><br />
<%= select_tag(:currency, options_from_collection_for_select(Currency.all, :id, :name),\
:id => "account_currency_id", :name => "account[currency][id]", :prompt => "Выберите валюту...")%>
</div>
<div class="actions">
<%= f.submit "Сохранить" %>
</div>
<% end %>
And when i'm trying to crate Account error occurred:
ActiveRecord::AssociationTypeMismatch in AccountsController#create
Currency(#52889580) expected, got ActiveSupport::HashWithIndifferentAccess(#28841960)
Request parameters:
{"utf8"=>"✓",
"authenticity_token"=>"VfCshuGyldoI5Q5DThT/RDpwewCh91apgsnmxyppWqI=",
"account"=>{"name"=>"Основной наличный счет",
"currency"=>{"id"=>"3"}},
"commit"=>"Save"}
If I try to find Currency from Id manually:
param = params[:account]
param[:currency] = Currency.find(param[:currency][:id])
@account = Account.new(param)
There are a new error by witch Name doesn't exists. And I don't like that i should manually set :id => "account_currency_id", :name => "account[currency][id]" because by default they both "currency".
Rails 3.1