I am beginner in ruby on rails. I ran into an error while using paperclip gem.
User model is
class User < ActiveRecord::Base
has_secure_password
attr_accessor :validate_password
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :replys, dependent: :destroy
has_attached_file :profile_picture, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
validates :registered_id, :presence => true, :uniqueness => true, :length => { :is => 10 },
:on => :create
validates :first_name, :presence => true
validates :last_name, :presence => true
validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
validates :password, :length => {:in => 6..20}, if: lambda { |u| u.validate_password == true }
end
and migration is generated by command line
"rails generate paperclip user profile_picture"
when I remove the line
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
in User model, it doesn't show the error. If I add that line It is showing error in
NoMethodError in Posts#index
Showing /home/akash/rails/forum/app/views/posts/index.html.erb where line #41 raised:
please help me solve this problem. Thanks in advance.
Hope you already follow all the beginning steps and ran migration there is nor :avatar
there is :profile_picture
as per your generator it will create attachment field for :profile_picture
From your code
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
should change to
validates_attachment_content_type :profile_picture, content_type: /\Aimage\/.*\Z/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.