I am using devise for authentication, and on the Sign Up page I have a text field for \'organization\' so when the user signs up, they will create an organization, and I want th
You can use accepts_nested_attributes_for
in your User model (documentation)
It should look like:
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
In views you could use Rails helper or create field by hand:
<input type="text" name="user[organization_attributes][name]">
<% user = User.new(organization => Organization.new) %>
<%= form_for user do |form| %>
<%= form.fields_for user.organization do |organization_form| %>
<%= organization_form.text_field :name %>
<% end %>
<% end %>
EDIT: Your devise view should look like:
<h2>Sign up</h2>
<% resource.organization ||= Organization.new %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<%= f.fields_for resource.organization do |organization_form| %>
<div><%= organization_form.label :name %><br />
<%= organization_form.text_field :name %></div>
<% end %>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>