Rails Accepted_nested_attributes_for 返回更新/插入子项的值
我是一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有帮助吗?
除非你向我们展示一些代码,否则很难给出直接的答案;)
更新:
不确定它是否会起作用,但值得尝试:
尝试使用
@pirate.attributes = params[:pirate]
而不是@pirate.update_attributes(params[:pirate])
然后,
does this help?
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,