Unpermitted parameters: avatar, avatar_cache

792 views
Skip to first unread message

Walter Colindres

unread,
Oct 22, 2013, 11:47:56 AM10/22/13
to carri...@googlegroups.com
I am attempting set up a user avatar per the documentation. When I try and create a new user with an avatar, it says it can not upload a blank field --- even though a file is selected. When I look at the output I see the following error:

Unpermitted parameters: avatar, avatar_cache

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

  def create
    @user = User.create(user_params)
  end

  private
  def user_params
    params.require(:user).permit( :email, :password, :remember_me, :avatar, :avatar_cache, :remove_avatar )
  end

  #attr_accessible :email, :password, :remember_me, :avatar, :avatar_cache, :remove_avatar

  validates_presence_of   :avatar
  validates_integrity_of  :avatar
  validates_processing_of :avatar
end


avatar_uploader.rb

# encoding: utf-8

class AvatarUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :scale => [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  # def extension_white_list
  #   %w(jpg jpeg gif png)
  # end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end


new.html.erb:

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:multipart => true}) do |f| %>
  <%= devise_error_messages! %>

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

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>

    <div>
      <%= image_tag(@user.avatar_url) if @user.avatar? %>

      <%= f.label :avatar do %>
          <%= f.file_field :avatar  %>
          <%= f.hidden_field :avatar_cache %>
      <% end %>

    </div>

  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>


Thanks in advance!

Walter Colindres

unread,
Oct 22, 2013, 9:11:34 PM10/22/13
to carri...@googlegroups.com
I figured it out. The documentation is pretty spot on, though a bit outdated in some aspects. I was placing my permitted values in the wrong file.

Robert Sefer

unread,
Feb 4, 2014, 5:17:10 PM2/4/14
to carri...@googlegroups.com
Walter, would you mind posting your solution? I am tearing my hair out trying to figure this same thing out. Thanks

Walter Colindres

unread,
Feb 6, 2014, 6:44:12 PM2/6/14
to carri...@googlegroups.com
I believe I had to move my permitted values into the controller. Let me look at it again and I will try to reply soon. It's been awhile since I played with it.

awl130

unread,
Apr 2, 2014, 9:20:56 PM4/2/14
to carri...@googlegroups.com
that's right, it should be in the controller, not the model
Reply all
Reply to author
Forward
0 new messages