gwalshington
gwalshington

Reputation: 1495

Nested Form for Join Table

I'm attempting to add a nested form into the Devise registration form. My 3 tables are Users (devise), Languages, and they both are joined in Languages_Users

It signs up the new user, but does not save Languages_Users, but I see that it is passed in the params. A new issue has also arisen - the avatar file is no longer saved. I'm not sure if these are related (if not, don't worry about addressing it).

**UPDATE - Here are my current logs - I now think the issue due to languages_users not receiving user_id

"languages users can't be blank'. In my logs though, it says 'SELECT 1 AS one FROM "languages_users" WHERE ("languages_users"."user_id" IS NULL AND "languages_users"."language_id" = 2) LIMIT 1' Is this an issue with not passing the user_id?

**

User.rb

class User < ActiveRecord::Base
  has_many :languages_users
  has_many :languages, :through => :languages_users
  accepts_nested_attributes_for :languages_users

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  belongs_to :role

  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"

  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
  validates_presence_of :first_name, :last_name, :location, :nationality, :bio
 end

registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
 def new
 build_resource({})
  resource.languages_users.build  
 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_user_attributes: [:id, :language_id, :user_id, :level]) }
end
end

in registrations: new.html.erb

<h2>Sign up</h2>

<%= 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>
  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>
  <div class="field">
    <%= f.label :location %><br />
    <%= f.text_field :location %>
  </div>
   <div class="field">
    <%= f.label :nationality %><br/>
    <%= f.text_field :nationality %>
  </div>
  <div class="field">
    <%= f.label :bio %><br />
    <%= f.text_field :bio %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="form-group">
        <%= f.label :avatar, class: 'col-sm-2 control-label'  %>
        <div class="col-sm-6">
            <%= f.file_field "user[avatar]" %>
        </div>
  </div>
    <%= f.fields_for :langauges_user do |lu| %>
    <br>
      <div class="fields"> 
      <%= lu.collection_select(:language_id, Language.order('language ASC').all, :id, :language) %><br>
      <%= lu.hidden_field :level, value: 1 %>
    <% end %>
  </div>  
  <br>
  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>
  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

logs:

    Started POST "/users" for ::1 at 2016-03-26 13:30:40 -0400
Processing by Users::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"x1F0kXqKN2/uu7S6BLxyBgSatcVWiSOLASEYYJ7ZF0b3d8V8O+FVQAO0yhjTJ2LImI+Xy4j7Rn+SvlYjV07mrA==", "user"=>{"first_name"=>"John", "last_name"=>"Smith", "location"=>"NYC", "nationality"=>"American", "bio"=>"hello", "email"=>"[email protected]", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f940b16a4f8 @tempfile=#<Tempfile:/var/folders/cn/l75pvjk9707bj93z_yykb0t40000gn/T/RackMultipart20160326-11607-1fg5ncg.JPG>, @original_filename="IMG_4573.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[user[avatar]]\"; filename=\"IMG_4573.JPG\"\r\nContent-Type: image/jpeg\r\n">}, "langauges_users"=>{"language_id"=>"16", "level"=>"1"}, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameters: user, langauges_users

Still a newb, so please let me know if you'd like me to provide anything further.

Thanks!

Upvotes: 0

Views: 253

Answers (1)

Pavan
Pavan

Reputation: 33542

You should change languages_user to languages_users

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: [:id, :language_id, :user_id, :level]) }
end

Also in fields_for

<%= f.fields_for :languages_users do |lu| %>

Upvotes: 1

Related Questions