Rails:在渲染部分时在 :object 中构建关联

发布于 2024-08-15 01:10:23 字数 1520 浏览 5 评论 0原文

我现在有一个看起来像这样的部分:

<%= 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 技术交流群。

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

发布评论

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

评论(3

蘑菇王子 2024-08-22 01:10:23

重构马特的答案:

def default_line_items
  line_items.build %w(editor photographer video_editor).collect { |i| {:title => i } }
end

一行。做同样的事情。

Refactor of Matt's answer:

def default_line_items
  line_items.build %w(editor photographer video_editor).collect { |i| {:title => i } }
end

One line. Does the same thing.

讽刺将军 2024-08-22 01:10:23

回答你改变的问题——

#order.rb
def default_line_items
  self.line_items.build(:title => "editor")
  self.line_items.build(:title => "photographer")
  self.line_items.build(:title => "video_editor")
  return self.line_items
end
#call to partial
render (:partial => "line_item", :collection => order.default_line_items)

In response to your changed question--

#order.rb
def default_line_items
  self.line_items.build(:title => "editor")
  self.line_items.build(:title => "photographer")
  self.line_items.build(:title => "video_editor")
  return self.line_items
end
#call to partial
render (:partial => "line_item", :collection => order.default_line_items)
胡渣熟男 2024-08-22 01:10:23

很抱歉,但这对我来说是一种严重的代码味道。违反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.

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