Have some time I’m stuck in this problem. I’m trying to pass 3 Id’s through a form to save the data in my database.
def new
@person = Person.find(params[:person])
@honored = Person.find(params[:honored])
@group = Group.find(params[:group_id])
@honor = Honor.new
end
def create
@person = Person.find(current_person)
@honor = Honor.create(:group => Group.find(params[:group_id]),
:person => Person.find(params[:person]),
:honored => Person.find(params[:honored]))
if @honor.valid?
flash[:success] = "Honor created."
redirect_to (:back)
else
redirect_to (:back)
end
end
In my view where I call the new method:
<% @asked_groupmembership.each do |agm| %>
<%= link_to "Create Honor", new_honor_path(:group_id => @group.id, :person => current_person.id,
:honored => agm.member.id) %>
My forms:
<% form_for @honor do |f| %>
<%= f.hidden_field :group_id, :value => @group.id %>
<%= f.hidden_field :person, :value => current_person.id %>
<%= f.hidden_field :honored, :value => @honored.id %>
<div class="field">
<%= f.label :texto %><br />
<%= f.text_field :texto %>
</div>
When I click the submit button I get this error:
Couldn't find Group without an ID
app/controllers/honors_controller.rb:22:in `create'
{"utf8"=>"✓",
"authenticity_token"=>"C7wofMY4VcyfdS10oc3iglex8nZVBMQ0Nh22nMiaOqs=",
"honor"=>{"group_id"=>"39",
"person"=>"2",
"honored"=>"44",
...
},
"commit"=>"Insert"}
How can I solve this?
Thanks.
##EDITED##
class Honor < ActiveRecord::Base
belongs_to :person, :class_name => 'Person', :foreign_key => "person_id"
belongs_to :honored, :class_name => 'Person', :foreign_key => "honored_id"
belongs_to :group, :class_name => 'Group', :foreign_key => "group_id"
You need to update your create method with:
Because after submitting your form you got your data in params[:honor]. You can see the hash passed to the controller in the code posted in your question: