Rails - Id can't be found in Forms

北城以北 提交于 2019-12-23 03:39:08

问题


Have some time I'm stuck in this problem. I'm trying to pass 3 Id's through a form to save the data in my database.

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

In my view where I call the new method:

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

When I click the submit button I get this error:

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

How can I solve this? Thanks.

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

回答1:


You need to update your create method with:

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

Because after submitting your form you got your data in params[:honor]. You can see the hash passed to the controller in the code posted in your question:

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



回答2:


In order or make it a little more readable let's do this

Form:

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

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

Every hidden field inside the form will reach the controller as a param inside the params[:honor]

Now, taking a look at the controller there's something that doesn't look quite right. The create method will create a new instance and save the object if there are no errors. Calling create and then save will work but you can either call new (instead of create) and then save or just keep using create and then valid?. Personally I think that the new then save process is cleaner.

Example:

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 


来源:https://stackoverflow.com/questions/6138153/rails-id-cant-be-found-in-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!