Reputation: 487
Here is the form view:
<% provide(:title, 'Create Brokerage') %>
<%= render partial: 'shared/errors', :locals => {:object => @brokerage}%>
<h1>Create User</h1>
<%= form_for @brokerage, url: {action: "create_brokerage"} do |f| %>
<%= f.fields_for :user do |builder| %>
<%= render 'users/form_elements', f: builder %>
<% end %>
<h1>Create Broker</h1>
<div class="form-group">
<%= f.label :about, class: 'sr-only' %>
<%= f.text_area :about, rows: 7, class: 'form-control', placeholder: 'About' %>
</div>
<div class="form-group">
<%= f.label :phone, class: 'sr-only' %>
<%= f.text_field :phone, class: 'form-control', placeholder: 'Phone Number' %>
</div>
<div class="form-group">
<%= f.label :email, class: 'sr-only' %>
<%= f.text_field :email, class: 'form-control', placeholder: 'Email Address' %>
</div>
<div class="form-group">
<%= f.label :image, 'Upload Image', class: 'btn btn-primary trigger' %>
<%= f.file_field :image, style: 'display:none;', class: 'uploadBtn' %>
</div>
<div class="form-group">
<%= f.submit 'Create Brokerage', class: 'btn btn-primary' %>
</div>
<% end %>
And users/_form_elements.html.erb
<div class="form-group">
<%= f.label :first_name, class: 'sr-only' %>
<%= f.text_field :first_name, class: 'form-control top-input', placeholder: 'First Name' %>
<%= f.label :last_name, class: 'sr-only' %>
<%= f.text_field :last_name, class: 'form-control bottom-input', placeholder: 'Last Name'%>
</div>
<div class="form-group">
<%= f.label :email, class: 'sr-only' %>
<%= f.text_field :email, class: 'form-control', placeholder: 'Email Address' %>
</div>
<div class="form-group">
<%= f.label :password, class: 'sr-only' %>
<%= f.password_field :password, class: 'form-control password-form top-input', placeholder: 'Password' %>
<%= f.label :password_confirmation, 'Confirmation', class: 'sr-only' %>
<%= f.password_field :password_confirmation, class: 'form-control password-form bottom-input', placeholder: 'Confirm Password' %>
</div>
Here is the create action:
def create_brokerage
@brokerage = Brokerage.new(brokerage_params)
@brokerage.first_name = params[:brokerage][:user][:first_name]
@brokerage.last_name = params[:brokerage][:user][:last_name]
clean_phone
if @brokerage.save
redirect_to login_path, notice: 'Broker was saved.'
else
render 'brokerages/signup'
end
end
private
def brokerage_params
params.require(:brokerage).permit(:first_name, :last_name, :about, :email, :phone, :image, :user => [:id, :first_name, :last_name, :email, :password, :password_confirmation])
end
brokerage.rb
has_many :users, through: :brokers
accepts_nested_attributes_for :users
broker.rb
class Broker < ActiveRecord::Base
belongs_to :brokerage
belongs_to :user
end
user.rb
has_many :brokerages, through: :brokers
When I save, output in the server:
Started POST "/brokerages/create_brokerage" for 127.0.0.1 at 2015-09-02 23:24:07 -0700
ActiveRecord::SchemaMigration Load (0.2ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by BrokeragesController#create_brokerage as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"FPgLuQX9yuv5kT/9OH9QOubMDhYvxcE/44ftpGmjAuep6GtlBqxc9avfEnMb2A1DzIbUSSy1jPxrO5UZOzxdow==", "brokerage"=>{"user"=>{"first_name"=>"123456", "last_name"=>"123456", "email"=>"123456", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "about"=>"ads", "phone"=>"1234567890", "email"=>"adasda"}, "commit"=>"Create Brokerage"}
Completed 500 Internal Server Error in 22ms
ActiveRecord::UnknownAttributeError (unknown attribute 'user' for Brokerage.):
app/controllers/brokerages_controller.rb:17:in `create_brokerage'
The brokerage is created but not the user or the broker relationship. What do I need to add so that the user can get created. I tried changing the brokerage_params
to:
def brokerage_params
params.require(:brokerage).permit(:first_name, :last_name, :about, :email, :phone, :image, user_attributes: [:id, :first_name, :last_name, :email, :password, :password_confirmation])
end
and got this in the server log:
Started POST "/brokerages/create_brokerage" for 127.0.0.1 at 2015-09-02 23:30:50 -0700
Processing by BrokeragesController#create_brokerage as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"FPgLuQX9yuv5kT/9OH9QOubMDhYvxcE/44ftpGmjAuep6GtlBqxc9avfEnMb2A1DzIbUSSy1jPxrO5UZOzxdow==", "brokerage"=>{"user"=>{"first_name"=>"123456", "last_name"=>"123456", "email"=>"123456", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "about"=>"ads", "phone"=>"1234567890", "email"=>"adasda"}, "commit"=>"Create Brokerage"}
Unpermitted parameter: user
Upvotes: 1
Views: 70
Reputation: 1601
Please change the user
to users
def create_brokerage
@brokerage = Brokerage.new(brokerage_params)
@brokerage.first_name = params[:brokerage][:users][:first_name]
@brokerage.last_name = params[:brokerage][:users][:last_name]
clean_phone
if @brokerage.save
redirect_to login_path, notice: 'Broker was saved.'
else
render 'brokerages/signup'
end
end
private
def brokerage_params
params.require(:brokerage).permit(:first_name, :last_name, :about, :email, :phone, :image, users_attributes: [:id, :first_name, :last_name, :email, :password, :password_confirmation])
end
UPDATE
<%= f.fields_for :users do |builder| %>
<%= render 'users/form_elements', f: builder %>
<% end %>
Upvotes: 1
Reputation: 7655
You probably need:
def brokerage_params
params.require(:brokerage).permit(:first_name, :last_name, :about, :email, :phone, :image, :user_attributes => [:id, :first_name, :last_name, :email, :password, :password_confirmation])
end
And fix your form as well:
<%= f.fields_for :users do |builder| %>
<%= render 'users/form_elements', f: builder %>
<% end %>
Upvotes: -1
Reputation: 854
it will be users_attributes
not user_attributes
in brokerage_params method. Have a look on https://github.com/ryanb/nested_form.
Thanks
Upvotes: 1