3

I have a form from user

<%= form_for(@user) do |f| %>
  <%= f.fields_for :businesses do |field| %>
    <div class="field">
      <%= field.label :address %>
      <%= field.text_field :address %>
    </div>   
    <div class="field">
      <%= field.label :city %>
      <%= field.text_field :city %>
    </div>  
  <% end %>
<% end %>

It does not display my fields, but when i change businesses to business, then it shows, or if I remove the f from f.fields_for. But I don't think it properly saves into database.

my user model

class User < ActiveRecord::Base
  has_many :businesses
  accepts_nested_attributes_for :businesses
en

my business model

class Business < ActiveRecord::Base
  attr_accessible :user_id, :address, :city
  belongs_to :user
end

my bussiness migration

class CreateBusinesses < ActiveRecord::Migration
  def change
    create_table :businesses do |t|
      t.integer :user_id
      t.string :address
      t.string :city

      t.timestamps
    end
  end
end

Any suggestions as to what I'm doing wrong?

Thanks

hellomello
  • 8,219
  • 39
  • 151
  • 297

1 Answers1

7

You should build a business before it can display a form for it:

@user.businesses.build

Use that before using fields_for

Also check out this great gem for managing nested forms:

https://github.com/ryanb/nested_form

Tim Baas
  • 6,035
  • 5
  • 45
  • 72
  • So user would need to have a business first before the form is displayed? But what if I want user to input a business? – hellomello Jun 11 '13 at 02:47
  • i was able to display the fields when I put `<%= f.fields_for :businesses, @user.businesses.build do |field| %>` But now when I save my database, the information doesn't show up in the field? Isn't my values should show up in the fields automatically if I want to edit my information? If I do `<%= @user.businesses %>` it shows my saved file. – hellomello Jun 11 '13 at 05:32
  • And it just keeps adding new data when I try to insert more information into `address` and `city` field, Its supposed to override the original data? – hellomello Jun 11 '13 at 05:33
  • I figured it out that I'm supposed to put the `@user.businesses.build` in my `new` action in users_controller – hellomello Jun 11 '13 at 05:54
  • Yes, thats right.. The `build` method works like `Model.new` but for an association. You can't create a form if the association isn't initialized. After you post the form you don't need to initialize it anymore, so you could use something like `@user.businesses.first || @user.businesses.build` to only build it if it doesn't exist. If you need more businesses to be added, you need to build again for the new ones. (Let the user select the amount of new businesses needed, and build them). But for a more user-friendly way, check out the plugin I mentioned.. – Tim Baas Jun 11 '13 at 07:33
  • I get a `undefined method `build' for nil:NilClass` when I add `@user.preferences.build`, why ?? – Augustin Riedinger Sep 13 '13 at 15:03
  • 2
    Found it : in a `has_one` relationship, it is `@user.build_preferences`. Found it thanks to this post: http://stackoverflow.com/questions/2472982/using-build-with-a-has-one-association-in-rails – Augustin Riedinger Sep 13 '13 at 15:12