gpt4 book ai didi

ruby-on-rails - rails : Creating a Multiple Model Form over n association levels

转载 作者:行者123 更新时间:2023-12-02 12:06:58 25 4
gpt4 key购买 nike

谁能告诉我为什么这个问题末尾的表单不能正常工作?

  • 保存不起作用
  • 选择帮助程序不会选择根据对象@kid
  • 的值

整个事情都是基于 Rails 2.2.2 的,不,升级到 Rails 2.3 来解决这个问题不是一个选择。 :-)

我用了this recipe构建多个模型表单。

# CLASS GRANDPARENT
class Grandparent < ActiveRecord::Base
has_many :parents
end

# CLASS PARENT
class Parent < ActiveRecord::Base
belongs_to :grandparent, :class_name => "Grandparent", :foreign_key => "grandparent_id"
has_many :kids
end

# CLASS KID
class Kid < ActiveRecord::Base
belongs_to :parent, :class_name => "Parent", :foreign_key => "parent_id"

# Virtual attribute setter for new self.parent.grandparent (Grandparent) attributes
def new_grandparent_attributes=(_gp_attributes)
self.parent.build_grandparent(_gp_attributes)
end

# Virtual attribute setter for existing self.parent.grandparent (Grandparent) attributes
def existing_grandparent_attributes=(_gp_attributes)
unless self.parent.grandparent.new_record?
attributes = _gp_attributes[self.parent.grandparent.id.to_s]
if attributes
self.parent.grandparent.attributes = attributes
else
self.parent.grandparent.delete(grandparent)
end
end
end

end

# CONTROLLER KIDS
class KidsController < ApplicationController
def new
@kid = Kid.new
end

def edit
@kid = Kid.find(params[:id])
end

def create
params[:kid][:new_grandparent_attributes] ||= {}
@kid = Kid.new(params[:kid])
end

def update
params[:kid][:existing_grandparent_attributes] ||= {}
@kid = Kid.find(params[:id])
end

end


# THIS IS THE MULTI-MODEL FORM USED IN THE VIEW

<% form_for(@kid) do |f| %>
<p>
<% new_or_existing = @kid.parent.grandparent.new_record? ? 'new' : 'existing' %>
<% prefix = "kid[#{new_or_existing}_grandparent_attributes][]" %>

<% fields_for prefix, @kid.parent.grandparent do |g_f| -%>
<p>
<%= g_f.label :, 'Grandparent Name' %><br />
<!-- THE FOLLOWING FORM DOESN'T CHANGE ACCORDING TO EXISTING @child -->
<%= @grandparents = Entity.find(:all, :order => :name)
g_f.collection_select(:name ,@grandparents, :id, :name)
%>
</p>
<% end %>
</p>
<p>
<%= f.label :name, "Kid Name" %><br />
<%= f.text_field :name %>
</p>
<%= submit_tag 'Go' %>
<% end %>

最佳答案

好吧,如果我错了,请纠正我,但看起来您实际上并没有在任何地方保存该对象。在创建和更新操作中,您调用 new,然后不保存它。

要纠正这个问题,你可以这样做:

def create
params[:kid][:new_grandparent_attributes] ||= {}
@kid = Kid.new(params[:kid])
if @kid.save
# successful save logic here
else
#failed save logic here
end
end

def update
params[:kid][:existing_grandparent_attributes] ||= {}
@kid = Kid.find(params[:id])
if @kid.update_attributes(params[:kid])
#successful save logic here
else
#failed save logic here
end
end

然后,在您的选择框中,您尝试查找 Entity 的每条记录,而不是与 @kid 相关的 Entity 的那些字段。为此,您必须在 child 和祖 parent 之间建立关系。

# CLASS GRANDPARENT
class Grandparent < ActiveRecord::Base
has_many :parents
has_many :grand_kids, :through => :parents
end

# CLASS PARENT
class Parent < ActiveRecord::Base
belongs_to :grandparent, :class_name => "Grandparent", :foreign_key => "grandparent_id"
has_many :kids
end

# CLASS KID
class Kid < ActiveRecord::Base
belongs_to :parent, :class_name => "Parent", :foreign_key => "parent_id"
belongs_to :grandparent

# ...

这样您就可以通过 @kid.grandparents 访问 child 的祖 parent 。然后您可以生成选择字段:

<%= g_f.collection_select(:name ,@kid.grandparents, :id, :name) %>

关于ruby-on-rails - rails : Creating a Multiple Model Form over n association levels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/705238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com