Complex forms in a simpler way?


#1

I have a form which updates across several models, and most of the
time what is covered is close to the railscast example (complex
forms), for adding photos etc, but i have another example trying to
use the field_for for a seemingly one to one relationship…

ie My form has a bunch of fields in one model, but two fields for
price both high and low in a second model. There can only be one
price or high and low added per form, but it is not a one to one
relationship because the price can change over time which is added
externally.

How can I do this?

Thanks,

Model:

class Price < ActiveRecord::Base
has_many :records
has_many :bubbles

Controller:

@price = Price.new
@price.bubbles.build

View:
<% form_for @price do |f| %>
<%= f.error_messages %>

<%= f.label :artist %>
<%= text_field_with_auto_complete :price, :artist, :autocomplete => "off", :size => "30" %>

..... .....
<% for bubble in @price.bubbles %>
<% fields_for "price[bubble]", bubble do |e| %>
    <p>
    <%= e.label :price %><br />
      <%= e.text_field :low, :size => "4" %>-<%=

e.text_field :high, :size => “4” %>


<% end %>
<% end %>

#2

Nevermind just did it thru the controller:

def new
@price = Price.new
@bubble = Bubble.new

def create
@price = Price.new(params[:price])
@bubble = @price.bubbles.build(params[:bubble])

view:
<% fields_for :bubble do |e| %>


<%= e.label :price %> $(10-25)

$<%= e.text_field :low, :size => “4” %>-<%=
e.text_field :high, :size => “4” %>


<% end %>