Rails - 在表单中找不到ID

时间:2011-05-26 12:02:41

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

有一段时间我遇到了这个问题。我正在尝试通过表单传递3个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.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

在我看来,我调用了new方法:

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

当我点击提交按钮时,我收到此错误:

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

我该如何解决这个问题? 感谢。

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

2 个答案:

答案 0 :(得分:2)

您需要使用以下命令更新您的创建方法

def create
  @person = Person.find(current_person)
  @honor = Honor.create(:group_id => params[:honor][:group],
           :person_id => params[:honor][:person],
           :honored_id => params[:honor][:honored])
 if @honor.save
 ...
end

因为在提交表单后,您获得了参数[:honor]的数据。您可以在问题中发布的代码中看到传递给控制器​​的哈希:

"honor"=>{"group"=>"39",
 "person"=>"2",
 "honored"=>"44",

答案 1 :(得分:0)

为了让它更具可读性,让我们这样做

形式:

<% form_for @honor do |f| %>
  <%= f.hidden_field :group_id, :value => @group.id %>
  <%= f.hidden_field :person_id, :value => current_person.id %>
  <%= f.hidden_field :honored_id, :value => @honored.id %>
  ...
<% end %>

控制器:

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[:honor][:group_id]),
    :person => Person.find(params[:honor][:person_id]),
    :honored => Person.find(params[:honor][:honored_id])
  )

  if @honor.save
    ...
  end
end

表单中的每个隐藏字段都将作为params[:honor]

中的参数到达控制器

现在,看看控制器,看起来不太合适。 create方法将创建一个新实例,并在没有错误时保存该对象。拨打create然后save即可,但您可以拨打new(而不是create),然后拨打save或继续使用create然后是valid?。就个人而言,我认为new然后save过程更清晰。

示例:

def create
  @person = Person.find(current_person)
  @honor = Honor.new(
    :group => Group.find(params[:honor][:group_id]),
    :person => Person.find(params[:honor][:person_id]),
    :honored => Person.find(params[:honor][:honored_id])
  )

  if @honor.save
    ...
  end
end