thoughtbot / clearance

Rails authentication with email & password.

Home | Edit | New

attr_accessible

User model uses attr_accessible. This is a best practice and Clearance is opinionated about it.

If you have any other attributes in your User, you will need to use attr_accessible on them, otherwise, they will not set during mass assignment. For example, if your model also has a ‘name’:

class User < ActiveRecord::Base
   include Clearance::User
end

class UsersController < Clearance::UsersController
  def create
    @user = Users.new(params[:user]) # what the?!? why isn't name getting set?
    # ... snip
  end 
end

To fix, just add the appropriate calls to attr_accessible:

class User < ActiveRecord::Base
   include Clearance::User
  attr_accessible :name
end

Using Paperclip?

You’ll need to use attr_accessible on all Paperclip attributes on your model. And don’t forget to use attr_accessible on the name of your attached file too.

So, here’s the basic User model from the Paperclip install guide.

class User < ActiveRecord::Base
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end

Once your add support for Clearance, it should look like this:

class User < ActiveRecord::Base
  include Permissions
  include Clearance::User
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }

  attr_accessible :avatar, :avatar_file_name, :avatar_content_type, :avatar_file_size, :avatar_updated_at
end

And that’s it. Now Paperclip works with Clearance, Clarence!

Last edited by mokolabs, Wed Dec 09 13:26:24 -0800 2009
Home | Edit | New
Versions: