Reputation: 1495
I have a nested form on my Devise registration form, which populates a join table. The join table information is not saving because user_id is nil. This is due to the fact that they are not signed in yet, so I can't pass current_user. How can I pass the user_id to save the join table (Languages Users) in one form?
relevant section of new.html.erb
<%= form_for(resource, as: resource, url: registration_path(resource)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
</div>
<%= f.fields_for :languages_users do |lu| %>
<div class="fields">
<%= lu.collection_select(:language_id, Language.order('language ASC').all, :id, :language) %><br>
<%= lu.hidden_field :level, value: 1 %>
<%#= lu.hidden_field :user_id, value: current_user.id %>
<% end %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
def new
build_resource({})
resource.languages_users.build(user_id: current_user)
respond_with self.resource
end
def create
super
end
private
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:email, :password, :password_confirmation, :bio, :location, :last_name, :first_name, :nationality, :avatar, languages_users_attributes: [ :language_id, :user_id, :level]) }
end
end
here are my params:
{"utf8"=>"✓",
"authenticity_token"=>"v/uM0+B7/XCUKmhyFGoEKqunCllXAK50uZN5f+TDpUsxlnIsgQVPbXhEb1q1P5pmG586iaULRROyarWzAzz12w==",
"user"=>{"first_name"=>"Walsh",
"last_name"=>"Costigan",
"location"=>"w",
"nationality"=>"United States",
"bio"=>"w",
"email"=>"[email protected]",
"user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007fdd25023f78 @tempfile=#<Tempfile:/var/folders/cn/l75pvjk9707bj93z_yykb0t40000gn/T/RackMultipart20160403-6431-1hpthwr.JPG>,
@original_filename="IMG_4574.JPG",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"user[user[avatar]]\"; filename=\"IMG_4574.JPG\"\r\nContent-Type: image/jpeg\r\n">},
"languages_users_attributes"=>{"language_id"=>"18",
"level"=>"1"},
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}
UPDATE** This is currently where my code is. It saves the user, but not the languages_user model
user.rb after_create :create_languages_user
def create_languages_user
@users = self.id
@languages_user = LanguagesUser.new(@user)
end
registrations controller:
def new
@user = User.new
build_resource({})
respond_with self.resource
end
def create
super
end
private
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:email, :password, :password_confirmation, :bio, :location, :last_name, :first_name, :nationality, :avatar, languages_users_attributes: [ :language_id, :level]) }
end
Upvotes: 3
Views: 243
Reputation: 585
You can do like this
after_create :create_languages_user
def create_languages_user
@languages_user = self.languages_users.create
end
Upvotes: 0
Reputation: 8122
It is simple. Just pass block to #create
method in Users::RegistrationsController
controller
def create
super do |user|
language_user_params = params[:user][:langauges_user].permit(:language_id,:level)
user.languages_users.create(language_id: language_user_params[:language_id], level: language_user_params[:level])
end
end
Upvotes: 3
Reputation: 4443
you can access the user_id
in an after_create
callback:
after_create :example_method
def example_method
NameOfModel.create(user_id: self.id, etc)
end
Upvotes: 2