Rails:在渲染部分时在 :object 中构建关联
我现在有一个看起来像这样的部分:
<%= render(:partial => 'order', :object => Order.new %>
How can I build a some empty LineItem object into the Order.new as in :object =>订单.新?
请注意,订单 has_many :line_items。 正如评论者提到的那样
,这乍一看似乎违反了 MVC 设计,但我忘了提及此渲染实际上位于 link_to_function 帮助程序中,该帮助程序用于动态插入属性行项目的更多字段。
实际的助手看起来像这样:
#orders_helper.rb
def add_line_item_link(name, form_scope)
link_to_function name, :class => "add_line_item_link" do |page|
line_item_html = render(:partial => 'line_item', :object => @order.line_items.new, :locals => {:f => form_scope})
page << %{
var time_index = new Date().getTime();
var line_item_html = #{line_item_html.to_json};
line_item_html = line_item_html.replace(/_\\d+/g, "_"+time_index);
line_item_html = line_item_html.replace(/\\[\\d+\\]/g, "\\["+time_index+"\\]");
$('line_items').insert({bottom: line_item_html});
}
end
end
@order.line_items.new 是我喜欢做的事情:
第一:我想要在 @order 对象中构建而不只是一个 line_item,我想要三个。 第二:订单项有一个名为“标题”的属性,每当我们收到订单时,几乎每次订单都恰好有三个订单项,一个具有标题编辑器,一个具有标题摄影师,一个具有标题视频编辑器。
所以我想,也许我可以这样:
#orders_controller.rb
@titles = %w(editor photographer video-editor)
#orders_helper.rb
...#same as above
:partial => 'line_items', :collection => lambda { @titles.each {|t| @order.line_items.build(:title => t) } return @order.line_items}
...
有什么建议吗? 谢谢
I have a partial now looking like this:
<%= render(:partial => 'order', :object => Order.new %>
How can I build a few empty LineItem object into the Order.new as in :object => Order.new?
Note that Order has_many :line_items. and LineItem belongs_to :order
And as a commenter mentioned, this might at first seem to violate the MVC design, but I forgot to mention that this render is really in a link_to_function helper which serves to dynamically insert more fields of the attribute line item.
The actual helper looks like this:
#orders_helper.rb
def add_line_item_link(name, form_scope)
link_to_function name, :class => "add_line_item_link" do |page|
line_item_html = render(:partial => 'line_item', :object => @order.line_items.new, :locals => {:f => form_scope})
page << %{
var time_index = new Date().getTime();
var line_item_html = #{line_item_html.to_json};
line_item_html = line_item_html.replace(/_\\d+/g, "_"+time_index);
line_item_html = line_item_html.replace(/\\[\\d+\\]/g, "\\["+time_index+"\\]");
$('line_items').insert({bottom: line_item_html});
}
end
end
@order.line_items.new is what I like to work on:
first: I want instead of just one line_item to be built in the @order object, I want three.
second: the line item has an attribute named 'title', and whenever we get an order, pretty much every time the order has exactly three line items, one has title editor, one has title photographer, and one has title video-editor.
So I thought, maybe I can so something like:
#orders_controller.rb
@titles = %w(editor photographer video-editor)
#orders_helper.rb
...#same as above
:partial => 'line_items', :collection => lambda { @titles.each {|t| @order.line_items.build(:title => t) } return @order.line_items}
...
any suggestions?
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
重构马特的答案:
一行。做同样的事情。
Refactor of Matt's answer:
One line. Does the same thing.
回答你改变的问题——
In response to your changed question--
很抱歉,但这对我来说是一种严重的代码味道。违反MVC原则。视图层不应该与模型层有任何直接交互。
I'm sorry but this is a serious code smell to me. Violation of MVC principles. The View layer should have no direct interaction with the Model layer whatsoever.