Adding nested attributes to devise user model
Forgot to add user_id to address migration. Rails expected it when adding belongs_to :user to the address model.
How do I use nested attributes with the devise model
You may be trying to mass assign some protected variable, OR you might not be saving a valid record. Check to make sure that the record is actually saving to the db.
Devise sign up with nested attributes make attributes required
You need to make changes in devise registrations views.
Generate views for both models: rails g devise:views users
and rails g devise:views profiles
.
Open registrations/new.html.erb
and add html5 required:true
attribute to required fields, ex. for email <%= f.email_field :email, required: true %>
When open your a nested model and be sure validation is existed for necessary fields, ex.: validates :first_name, presence: true
.
Rails 4.0 with Devise. Nested attributes Unpermited parameters
config/routes.rb
Create your own registration controller like so ... (see Devise documentation for the details of overriding controllers here ...) ... which is more elegant way as opposed to doing it via the ApplicationController
devise_for :users, controllers: {registrations: 'users/registrations'}
app/controllers/users/registrations_controller.rb
Override the new method to create a Profile
associated with the User
model as below ... run the configure_permitted_parameters
method before to sanitize the parameters (note how to add nested parameters)
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
# GET /users/sign_up
def new
# Override Devise default behaviour and create a profile as well
build_resource({})
resource.build_profile
respond_with self.resource
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:email, :password, :password_confirmation, :profile_attributes => :fullname)
}
end
end
db/migrate/xxxxxxxxxxxxxx_create_profiles.rb
This is the migration that generates the Profile
model (note the reference to User
) ... this example profile only keeps fullname
as an extension of the User
but feel free to add as you wish!
class CreateProfiles < ActiveRecord::Migration
def change
create_table :profiles do |t|
t.references :user
t.string :fullname
t.timestamps
end
end
end
app/models/user.rb
class User < ActiveRecord::Base
# Associations
has_one :profile, dependent: :destroy, autosave: true
# Allow saving of attributes on associated records through the parent,
# :autosave option is automatically enabled on every association
accepts_nested_attributes_for :profile
# Devise
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
app/models/profile.rb
class Profile < ActiveRecord::Base
# Associations
belongs_to :user
# Validations
validates :fullname, presence: true
end
app/views/devise/registrations/new.html
<% resource.build_profile if resource.profile.nil? %>
<%= form_for(resource, :as => resource_name,
:url => registration_path(resource_name)) do |f| %>
<ul>
<%= devise_error_messages! %>
<li class="fullname">
<%= f.fields_for :profile do |profile_fields| %>
<%= profile_fields.label :fullname %>
<%= profile_fields.text_field :fullname %>
<% end %>
</li>
<li class="email">
<%= f.label :email %>
<%= f.email_field :email, :autofocus => true %>
</li>
<li class="password">
<%= f.label :password %>
<%= f.password_field :password %>
</li>
<li class="password">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</li>
<li>
<%= f.submit %>
</li>
<li>
<p><%= render "devise/shared/links" %></p>
</li>
</ul>
<% end %>
Allowing a nested attribute for a User with Devise in Rails 4
These are nested attributes, so you need to add them in following way:
devise_parameter_sanitizer.for(:sign_up) { |u|
u.permit(:username, :staff_attributes => [:name, :position, :etc])
}
Rails + Devise, registration form with nested attributes not creating associated record
It seems you are using Rails 5, belongs_to
requires association record must be exist by default. If so, then you need to add inverse_of
to your associations:
class Guest
has_one :invite, inverse_of: :guest
end
class Invite
belongs_to :guest, inverse_of: :invite
end
Rails nested Attributes with Devise (Signup form)
Ah interesting - it seems the error was in my bookings model where I was validating lead_id, presence: true. Removing this fixes the issue. Thanks for the help everyone!
Related Topics
Instagram Ruby Gem - Unable to Reach Callback Url
No Such File to Load -- Bundler/Setup in Rails 3.2 with Passenger
How to Use 'Debugger' and 'Pry' When Developing a Gem? (Ruby)
Enter & Ioerror: Byte Oriented Read for Character Buffered Io
Certificate to Pem to Certificate Not Working: Nested Asn1 Error
How to Add 'Each' Method to Ruby Object (Or Should I Extend Array)
Intermingling Attr_Accessor and an Initialize Method in One Class
Passing Hashes Instead of Method Parameters
Ide Sublime2 How to Find Method Definition
How to Output My Ruby Commandline Text in Different Colours
Is It a Bad Idea Do Divide the Models into Directories
Differencebetween Gsub and Sub Methods for Ruby Strings