This question is about a different approach I'm trying to the one asked here:
Passing IDs through a new Form
I have a group#view page
, that is accessed by a Person
. In this page, the Person
can see the members of the group via methods I developed. The problem is that I need to create the model Honors
using the Id from the group
, the id from the person
accessing the page, and the id from a member
of this group
.
In my Honors controller I have:
def create
@person = Person.find(current_person)
@asked_groupmembership = @person.owned_group_memberships.find_all_by_status(true,:include => [:group, :member])
@asked_groupmembership.each do |agm|
@honor = Honor.create(:group => Group.find(params[:group_id]),
:person => Person.find(current_person), :honored => Person.find(agm.member.id))
end
if @honor.save
...
end
In my view I have a link that directs the person
to the form in order to create a new honor:
<% @asked_groupmembership.each do |agm| %>
<%= link_to "Create Honor", new_honor_path(:group_id => @group.id, :person => current_person.id,
:honored => agm.member.id) %>
But in my forms I can't get the ids and stuff
<% form_for(:开发者_JAVA技巧honor, :url => honors_path(:group_id, :person,
:honored)) do |f| %>
The error I get is that I can't find Group without an Id.
Any ideas? Thanks.
##Edited2##
Changed my crontroller
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.save
...
end
First, it seems like you are missing a controller method. Along with every form that creates a new object there are typically two controller methods
new
- Gathers up any data that the form needs to render itself
- Renders the form
create
- Collects the data from the form
- Creates the new object
It looks to me like you are missing the new method. In the new method you would gather up all the hidden data that the form needs (e.g. the information that the user is not going to type in directly, like the @person info). Then, I would put this information in your form using hidden form parameters (rather then trying to put it in the form URL).
Objective: From the group#view, loop through and create and "Add Honor" link for each member of the group, while keeping track of the specific member being honored, group in which said member is honored, and the person who is honoring.
The following accomplishes that objective.
Route:
match "honors/:group/:person/:honored" => "honors#create", :as=>"my_custom_new_honor"
Group view:
<% @asked_groupmembership.each do |agm| %>
<%= link_to "Create Honor", my_custom_new_honor_path(:group=> @group.id, :person => current_person.id,:honored => agm.member.id) %>
Honor Controller, Create Method
@honor = Honor.create(:group => Group.find(params[:group_id]),
:person => Person.find(params[:person]),
:honored => Person.find(params[:honored]))
In this scenario you do not need the honor#new method, you're going directly to the create method. This assumes all the relationships are established correctly.
精彩评论