本文介绍了Rails-奇怪的表单:不保存任何text_field的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题由Matteo Alessani在>轨道-ID无法在表单中找到,我注意到我的表单没有保存我传递的字段.

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:

路线:

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>

错误是我可以从 group person 以及 honored 那里获得ID,但是我没有输入表格(我的属性是葡萄牙语,所以我不会翻译):

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.

谢谢!

推荐答案

您在控制器中有误

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

这篇关于Rails-奇怪的表单:不保存任何text_field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:00