Reputation: 279
I have a Stage Model, which belongs_to User Model. User Model has_many Stages.
When I try to create a New Stage, with an hidden_field containing the id of current_user
,
I have the following error :
ActiveRecord::AssociationTypeMismatch in StagesController#create
User(#63609336) expected, got String(#16545012)
Here's the array :
{"utf8"=>"✓",
"authenticity_token"=>"YD3VVr9Hntg/LEGjFAzu2roR5NGe6FPclew+zJOvGpY=",
"stage"=>{"user"=>"8",
"intitule"=>"er",
"entreprise"=>"er",
"secteur"=>"er",
"mission"=>"er",
"duree"=>"erea",
"annee_debut"=>"az",
"annee_fin"=>"z",
"comment"=>"az",
"site_entreprise"=>"az"},
"commit"=>"Create Stage"}
user_id
is an Integer field.
The view :
<%= f.hidden_field :user, :value => current_user.id %>
Where is the problem ? I can't find any solution ! Thanks for your help.
Upvotes: 0
Views: 81
Reputation: 25029
I wouldn't recommend using hidden fields for this, as hidden fields can be modified by the user. I recommend doing something like current_user.stages.create
in the controller instead, which will pre-populate the user_id in the created Stage.
Upvotes: 1
Reputation: 8169
replace with
<%= f.hidden_field :user_id, :value => current_user.id %>
Upvotes: 2