1

I'm doing the onemonth rails and I'v got a problem with the attr_accessible function. I've installed it as a gem in rails 4(gem 'protected_attributes') and using it with the simple_form.

But the problem is that when I update my form with a name, it doesnt remember it! But it says it updated successfully??

Ths is my user.rb

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, :name end 
2
  • remove the entire line of attr_accessible and use strong parameters in your user controller Commented Oct 26, 2014 at 10:37
  • I generated the users controller, but I don't understand what I have to put in it, to make it work? Commented Oct 26, 2014 at 12:38

1 Answer 1

3

Since you are using Devise you can remove the entire attr_accessible line (and the strong_parameters gem, see more below). Devise provides a controller which handles sign-up for you already.

If you want to add other attributes to your user you can subclass Devise::RegistrationsController with your custom controller:

# app/controllers/registrations_controller class RegistrationsController < Devise::RegistrationsController private def sign_up_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end def account_update_params params.require(:user).permit(:name, :email, :password, :password_confirmation, :current_password) end end 

You then need to tell Devise to route to your custom controller:

# config/routes.rb devise_for :users, :controllers => { registrations: 'registrations' } 

PS. I would also recommend removing the strong_parameters gem and use the out of the box Rails 4 strong parameters. There are several known problems with whitelisting parameters on the model level (different params for different actions for example).

Sign up to request clarification or add additional context in comments.

4 Comments

Also here is an example of an app which uses Devise and simple_form.
you said that devise provides the controller, but I cant find it under /devise / app / controllers / devise / registrations_controller.rb/ do I have to generate it myself ou did I miss something? Tnx Tom
Yes, but it is in the Devise gem - not in your application code. You can find the location by running gem which devise.
Tnx man, I did just that and it worked:) I generated the controlelr and made the route to it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.