Rails - 表现形式奇怪:不保存任何text_field

时间:2011-05-26 13:34:52

标签: ruby-on-rails ruby-on-rails-3 forms

Matteo Alessani在Rails - Id can't be found in Forms中解决了我的问题之后,我注意到我的表单没有保存我通过的字段。

我将在这里复制我从另一个问题得到的所有代码:

路线:

resources :honors

型号:

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"

控制器:

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

在视图中:

<% @asked_groupmembership.each do |agm| %>
<%= link_to "Create Honor", new_honor_path(:group_id => @group.id,
 :person => current_person.id, :honored => agm.member.id) %>   

我的表格:

<% 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>

错误是我可以从groupperson以及honored获取ID,但我在表单中输入的内容都没有(我的属性是葡萄牙语,所以我不会翻译):

 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".

注意:日志中的参数带有值。

谢谢!

1 个答案:

答案 0 :(得分:2)

你在控制器中有错误

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