Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Simple Ruby on Rails Application Creating an Authentication System Generating the Devise Views

Rodrigo Soares
Rodrigo Soares
2,460 Points

Attributes are not being assigned to table, even if columns are created

I'm following the treebook tutorial development and can't find out why in the sign-up page, even setting the attributes :first_name, :last_name and :profile_name, those attributes don't save in my user table...

The columns exists as I just ran a rails console command to check it out:

Rodrigos-MacBook-Air:treebook RodrigoSoares$ rails console
Loading development environment (Rails 4.0.3)
irb(main):001:0> user = User.first
  User Load (0.3ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 2, first_name: nil, last_name: nil, profile_name: nil, email: "roderswiss@outlook.com", encrypted_password: "$2a$10$0GEVhQyhRChkaUwcVl3tS.o9fOXqnlbBwjBO8e3VA46t...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-02-25 15:34:50", last_sign_in_at: "2014-02-25 15:34:50", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-02-25 15:34:50", updated_at: "2014-02-25 15:34:50">

This is my view for New Signup:

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :first_name %><br />
    <%= f.text_field :first_name, :autofocus => true %></div>

  <div><%= f.label :last_name %><br />
  <%= f.text_field :last_name %></div>

  <div><%= f.label :profile_name %><br />
  <%= f.text_field :profile_name %></div>

  <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

  <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 "devise/shared/links" %>

and this is my model for User:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

   attr_accessible :email, :password, :password_confirmation, :remember_me,
                   :first_name, :last_name, :profile_name
end

Even if I edit the user the attributes aren't assigned to my entry...

I would kindly ask you to help me out.

Thanks a lot!

8 Answers

I have similar issue as yours but slightly different. when I fill out the registration form and click "sign up", I get the following error: Sign up 5 errors prohibited this user from being saved: Email has already been taken First name can't be blank Last name can't be blank Profile name can't be blank Profile name has already been taken

The new user get's created (I can see that from the console). but I'm getting these errors. I'm not sure what it is.

Rodrigo Soares
Rodrigo Soares
2,460 Points

It seems like you have created an user that is in the DB Users table already. You will have to delete it in order to creat it again.

Run this commands to see if it helps you out:

''' rails console ''' ''' user = User.first ''' See if it brings you the user

''' user.destroy '''

On the blank issues I would have to see the views your Users table migration and status model.

Ben Wong
Ben Wong
2,652 Points

I have the same issue. Did you ever find a fix for this? Thanks.

first thing first: if you are using Rails 4, you need attr_accessible attributes in your model. atributes are now protected in the controller using the params hash. see this post: http://stackoverflow.com/questions/17371334/how-is-attr-accessible-used-in-rails-4

I know this doesn't answer your question. I don't think it has nothing to do with putting attributes in the model or controller. Right now if you look at your irb output, you'll see all of the attributes are nil except the email address. So just to make sure users are being created properly, try assigning values to all the attributes of the user that are nil. then call user.valid? to validate it.

when you create a new user in the singup page, and click Sing up, are you getting an error message or success message?

Rodrigo Soares
Rodrigo Soares
2,460 Points

hello Wali,

I get a success message. The values seems to be assigned when I register our edit profile whoever it's not assigning it as I showed above...

I'm trying to restart from scratch the installation of Devise. Maybe I did something wrong when migrating the DB I'll take a close look to it.

Rodrigo, I posted my views and model in a new post under Ruby titled: Treebook app: First_name, Last_name, profile_name nat savingtled

I'll include migration table under it. let me know what you think. thanks

Justin Banta
Justin Banta
3,834 Points

Hey Rodrigo, I was having a similar problem. You'll want to google strong parameters and something called parameter sanitization. There's a number of good resources, here are two that I found the most helpful: http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/ https://github.com/rails/strong_parameters/ good luck.

Ben Wong
Ben Wong
2,652 Points

I have the issue that Sign up 5 errors prohibited this user from being saved: Email has already been taken First name can't be blank Last name can't be blank Profile name can't be blank Profile name has already been taken.

I suspect it's the parameters sanitization since I'm using Rails 4.

Devise doesn't generate a controller.

What's the fix?

Thanks.

Ben Wong
Ben Wong
2,652 Points

Alright. I got it. Add this to Application controller. Per the documentation. https://github.com/plataformatec/devise

before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation) } end

Ben Wong
Ben Wong
2,652 Points

Alright. I got it. Add this to Application controller. Per the documentation. https://github.com/plataformatec/devise

before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation) } end