Rails Accepted_nested_attributes_for 返回更新/插入子项的值

发布于 2024-11-02 12:46:17 字数 1795 浏览 0 评论 0原文

我是一个 stackoverflow 新手。请仁慈一点;-)

我与accepts_nested_attributes_for 有一个简单的has_many 关联。一切正常。但我真正需要的是更新和/或插入的子项的返回值,因为我通过 Ajax 发送表单,并且在成功发布/放置后只需要操作这些子项的表单字段。

编辑:

模型:

class Pirate < ActiveRecord::Base
  has_many :ships
  accepts_nested_attributes_for :ships, :reject_if => proc { |a| a[:name].blank? }, :allow_destroy => true
end

class Ship < ActiveRecord::Base
  belongs_to :pirate
end

控制器:

def new
  @pirate = Pirate.new
  4.times { @pirate.ships.build }
end

def edit
  @pirate = Pirate.find(params[:id])
end

def create
  @pirate = Pirate.new(params[:pirate])

  respond_to do |format|
    if @pirate.save
      format.js
    else
      format.js
    end
  end
end

def update
  @pirate = Pirate.find(params[:id])

  respond_to do |format|
    if @pirate.update_attributes(params[:pirate])
      format.js
    else
      format.js
    end
  end
end

视图:

<%= form_for @pirate, :remote => true do |f| %>
  <%= f.label :name, "Pirates Name" %>
  <%= f.text_field :name %>

  <%= f.fields_for :ships do |ship| %>
    <
    <%= f.label :name, "Ship Name" %>
    <%= ship.text_field :name %>
  <% end %>

  <%= f.submit %>
<% end %>

update.js.erb:

<% @pirate.only_updated_ships.each do |u| %>
  alert("<%= u.id %>");
  do something...
<% end %>

<% @pirate.only_newly_inserted_ships.each do |i| %>
  alert("<%= i.id %>");
  do something...
<% end %>

所以,我在这里有一个远程调用以及一个create.js.erb和update.js.erb,我想在其中处理返回的更新和/或插入的子对象。

问题是您只能将 @pirate 作为成功发布/放置的返回值。调用@pirate.children 当然会给你所有的孩子。

希望我的问题现在更清楚了吗?

I'am a stackoverflow newbie. Please be merciful ;-)

I have a simple has_many association with accepts_nested_attributes_for. Everything works fine. But I what I really need are the return values for the updated and/or inserted children, because I send the form per Ajax and need to manipulate only these children's form fields after successfully post/put.

EDIT:

Models:

class Pirate < ActiveRecord::Base
  has_many :ships
  accepts_nested_attributes_for :ships, :reject_if => proc { |a| a[:name].blank? }, :allow_destroy => true
end

class Ship < ActiveRecord::Base
  belongs_to :pirate
end

Controller:

def new
  @pirate = Pirate.new
  4.times { @pirate.ships.build }
end

def edit
  @pirate = Pirate.find(params[:id])
end

def create
  @pirate = Pirate.new(params[:pirate])

  respond_to do |format|
    if @pirate.save
      format.js
    else
      format.js
    end
  end
end

def update
  @pirate = Pirate.find(params[:id])

  respond_to do |format|
    if @pirate.update_attributes(params[:pirate])
      format.js
    else
      format.js
    end
  end
end

View:

<%= form_for @pirate, :remote => true do |f| %>
  <%= f.label :name, "Pirates Name" %>
  <%= f.text_field :name %>

  <%= f.fields_for :ships do |ship| %>
    <
    <%= f.label :name, "Ship Name" %>
    <%= ship.text_field :name %>
  <% end %>

  <%= f.submit %>
<% end %>

update.js.erb:

<% @pirate.only_updated_ships.each do |u| %>
  alert("<%= u.id %>");
  do something...
<% end %>

<% @pirate.only_newly_inserted_ships.each do |i| %>
  alert("<%= i.id %>");
  do something...
<% end %>

So, I have a remote call here and a create.js.erb and update.js.erb where I want to work with the returned updated and/or inserted children objects.

The problem is that you only get the @pirate as a return value for the successful post/put. And calling @pirate.children will give you of course all children.

Hope my problem is clearer now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ弥猫深巷。 2024-11-09 12:46:17

这有帮助吗?

u = User.first
u.login #=> 'hello'
params[:user] #=> { :login => 'world' }
u.attributes = params[:user]
u.login #=> 'world'. Other attributes are the same as before 
u.changed #=> ["login"]

除非你向我们展示一些代码,否则很难给出直接的答案;)

更新:

不确定它是否会起作用,但值得尝试:
尝试使用 @pirate.attributes = params[:pirate] 而不是 @pirate.update_attributes(params[:pirate])
然后,

@only_updated_ships = @pirate.ships.find_all { |ship| ship.changed? }
@only_updated_ships.each { |ship| ship.save }

does this help?

u = User.first
u.login #=> 'hello'
params[:user] #=> { :login => 'world' }
u.attributes = params[:user]
u.login #=> 'world'. Other attributes are the same as before 
u.changed #=> ["login"]

It's just somewhat difficult to give a straight answer unless you show us some code ;)

UPDATE:

Not sure if it's going to work, but it worth trying:
try @pirate.attributes = params[:pirate] instead of @pirate.update_attributes(params[:pirate])
then,

@only_updated_ships = @pirate.ships.find_all { |ship| ship.changed? }
@only_updated_ships.each { |ship| ship.save }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文