Rails - Forms acting Weird: not saving any text_field
After getting my question solved by Matteo Alessani in Rails - Id can't be found in Forms, I noticed that my form isn't saving the fields I pass.
I will copy here all the piece of code I have from the other question:
Routes:
resources :honors
Model:
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"
Controller:
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.save(:group_id => params[:honor][:group],
:person_id => params[:honor][:person],
:honored_id => params[:honor][:honored])
if @honor.valid?
flash[:success] = "Honor created."
redirect_to (:back)
else
redirect_to (:back)
end
end
In the view:
<% @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 %>
<%开发者_StackOverflow= 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>
And the error is that I can get the ID's from group
and person
and the honored
one, but nothing that I type in the forms (my attributes are in portuguese so I won't translate):
INSERT INTO "honors" ("group_id", "person_id", "honor_id", "texto", "nota",
"nivel_habilidade", "comprometimento", "tempo_demora",
"criatividade", "organicazao", "comunicacao", "trabalho_grupo", "created_at",
"updated_at") VALUES (39, 2, 44, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, '2011-05-26 12:58:56.433510', '2011-05-26 12:58:56.433510')
RETURNING "id".
Note: the Parameters in log are with the values.
Thanks!
You have mistake in controller
def create
@person = Person.find(current_person)
@honor = Honor.new(params[:honor])
if @honor.save
flash[:success] = "Honor created."
redirect_to (:back)
else
redirect_to (:back)
end
end
精彩评论