Forum: Ruby on Rails complex forms in a simpler way?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
holden (Guest)
on 2009-05-14 13:14
(Received via mailing list)
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 %>

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

    <% 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" %>
        </p>
      <% end %>
  <% end %>
holden (Guest)
on 2009-05-14 17:18
(Received via mailing list)
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| %>
        <p>
        <%= e.label :price %> $(10-25)<br />
          $<%= e.text_field :low, :size => "4" %>-<%=
e.text_field :high, :size => "4" %>
        </p>
      <% end %>
This topic is locked and can not be replied to.