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
max
  • 96,212
  • 14
  • 104
  • 165
Tomislav Mikulin
  • 5,306
  • 4
  • 23
  • 36

1 Answers1

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).

max
  • 96,212
  • 14
  • 104
  • 165
  • Also here is an [example of an app](https://github.com/remote-wind/remote-wind) which uses Devise and simple_form. – max Oct 26 '14 at 13:06
  • 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 – Tomislav Mikulin Oct 28 '14 at 10:18
  • Yes, but it is in the Devise gem - not in your application code. You can find the location by running `gem which devise`. – max Oct 28 '14 at 19:22
  • Tnx man, I did just that and it worked:) I generated the controlelr and made the route to it! – Tomislav Mikulin Oct 30 '14 at 16:55