Rails:将新的子对象占位符(构建)传递给父视图
我有 2 类对象...Magician has_many Rabbits
和 Rabbit owns_to Magician
。
查看魔术师 (show.html
) 时,我想列出所有关联的兔子,然后使用一些带有提交按钮的空白字段来添加新兔子。为此,我在魔术师的 show
方法(如下)中构建一只新兔子(与当前魔术师关联)。
Edit2:找到了使其工作的方法,但不确定它是否是“Rails 方式”?
查看内嵌评论(如下): 如果我在 Magician 的 show
方法中构建兔子,那么当渲染 show
时,一个空(且无效)的兔子会在显示新的兔子表单字段之前结束列表。
如果我在视图本身中构建它,那么一切都会正常工作。正确渲染。我被引导相信我们不应该在视图中做这种类型的事情...如果是这样,解决这个问题的正确方法是什么?
#/app/controllers/magicians_controller.rb
class MagiciansController < ApplicationController
respond_to :html, :json
def show
@magician = Magician.find(params[:id])
@rabbit = @magician.rabbits.build # <=== build here and empty rabbit gets
# included in @magician.rabbits when they're rendered...
# but is NOT included in @magician.rabbits.count for some reason?!?!?
respond_with(@magician)
end
...
end
#view/magicians/show.html.haml
%p
%b Name:
= @magician.name
%h2 Rabbits
= "There are #{pluralize(@magician.rabbits.count, "rabbit")}"
= render @magician.rabbits, :target => @magician
%h2 Add a rabbit:
- @rabbit = @clown.rabbits.build -# <=== build here and everything SEEMS to work
= render :partial =>"rabbits/form", :locals => { :parent => @magician, :foreign_key => :magician_id, :flash => flash }
Edit1 :根据请求添加从部分生成的html:
<p>
<b>Rabbit:</b>
<b>Color:</b>
|
<b>ID:</b>
<a href="/magicians/2/rabbits" data-confirm="Sure? A bunny will die" data-method="delete" rel="nofollow">Kill Rabbit</a>
</p>
我想您可能想查看生成此内容的部分:
%p
%b Rabbit:
= rabbit.name
%b Color:
= rabbit.color
|
%b ID:
= rabbit.id
= link_to("Kill Rabbit", [target, rabbit], :method => :delete, :confirm => "Sure? A bunny will die")
I've got 2 classes of objects... Magician has_many Rabbits
and Rabbit belongs_to Magician
.
When viewing a Magician (show.html
) I'd like to list all the associated Rabbits and then have some blank fields with a submit button to add a new Rabbit. To do so I build a new rabbit (associated to the current magician) in the Magician's show
method (below).
Edit2: found way to make it work but not sure if it's the "Rails way"?
see comments inline (below):
If I build the rabbit in Magician's show
method then when show
is rendered an empty (and invalid) rabbit ends the list before the new rabbit form fields are then shown.
If I build it in the view itself then everything works & renders correctly. I was led to believe that we should not be doing this type of stuff in the view...if so, what's the proper way to address this?
#/app/controllers/magicians_controller.rb
class MagiciansController < ApplicationController
respond_to :html, :json
def show
@magician = Magician.find(params[:id])
@rabbit = @magician.rabbits.build # <=== build here and empty rabbit gets
# included in @magician.rabbits when they're rendered...
# but is NOT included in @magician.rabbits.count for some reason?!?!?
respond_with(@magician)
end
...
end
#view/magicians/show.html.haml
%p
%b Name:
= @magician.name
%h2 Rabbits
= "There are #{pluralize(@magician.rabbits.count, "rabbit")}"
= render @magician.rabbits, :target => @magician
%h2 Add a rabbit:
- @rabbit = @clown.rabbits.build -# <=== build here and everything SEEMS to work
= render :partial =>"rabbits/form", :locals => { :parent => @magician, :foreign_key => :magician_id, :flash => flash }
Edit1: Adding generated html from partial as per request:
<p>
<b>Rabbit:</b>
<b>Color:</b>
|
<b>ID:</b>
<a href="/magicians/2/rabbits" data-confirm="Sure? A bunny will die" data-method="delete" rel="nofollow">Kill Rabbit</a>
</p>
And I suppose you probably want to see the partial that generates this:
%p
%b Rabbit:
= rabbit.name
%b Color:
= rabbit.color
|
%b ID:
= rabbit.id
= link_to("Kill Rabbit", [target, rabbit], :method => :delete, :confirm => "Sure? A bunny will die")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论