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