Ruby on rails - paperclip not saving to database
Are you using Resque
for background job,if yes then u need to start it using rake resque:work QUEUE='*'
.in rails usually Resque
is used to handle background jobs which involves mailer and picture upload.OR an example below for product.html.erb having partial for photos upload for that product with paperclip configures with Amazon S3.
product.html.erb
<%= render :partial => 'photos' %>
_photos.html.erb for atleast one image mandatory
<% if @product.photos[0].nil? %>
<a href="javascript:void(0);" class="add-photos" >
<img src="/assets/default/product-add-photos.png" alt="Add product photos"/>
</a>
<% end %>
<img src="<%= (@product.product_photos[0].nil? ? "" : @product.photos[0].image.url(:small)) %>" id="photos_1" class="product-photos-src <%=@product.photos[0].nil? ? 'dontdisplay' : ''%> "/>
Rails: paperclip does not save in database
You don't need to set attr_accessible
on those subfields. The only thing you need to mark as attr_accessible
is the :photo
attribute:
class UsersPicture < ActiveRecord::Base
attr_accessible :user_id, :users_picture_file, :users_picture_id, :photo
Rails: Paperclip Not Saving Image to DB
You are using :image
for file_field
but your paperclip field is using product_image
, change as follow to upload file
<div class="form-group col-xs-12">
<%= f.file_field :product_image, as: :file %>
</div> <!-- form group -->
Ruby on rails-paperclip do not save to the database
Step by Step
Gem
gem "paperclip", "~> 3.0"
Terminal
$ bundle install
$ rails generate paperclip user image
$ rake db:migrate
User model
class User < ActiveRecord::Base
attr_accessible:image, :description
validates :description, presence: true
validates :user_id, presence: true
has_attached_file :image, styles: { medium: "320x240>"}
validates_attachment :image, presence: true,
content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] },
size: { less_than: 5.megabytes } # you wanna change that
belongs_to :user
end
Your View is ok, don't forget to rake db:migrate
and restart your Server
Paperclip not saving file and attachment
I believe you were talking about the avatar in products.rb.
You are missing avatar in the strong parameters of the related controller:
params.require(:product).permit(:avatar, :name, :number, :image, :availability, :prize, :status, :edition)
EDIT : I've read your code more in detail, and there is something you don't understand about the MVC model.
You're supposed to put in your models only the list of fields/attributes you have (I don't like ActiveRecord for this reason, because these fields are "hidden" inside your database schema, unlike Mongoid)
The controller are what links the Views (the HTML the user sees) and the models. Because a user could at any time modify the POST request sent by his browser, since Rails 4 "strong parameters" have become the default option. Basically, you have to explicitely tell your application what form parameters are allowed, so a malicious user cannot, for example, set a :superadmin
column to true for his user.
Therefore, lines like
def product_params
params.require(:product).permit(:avatar)
end
have absolutely no meaning inside your models. They should be in your controller only.
params
is the name of the variable that contains the POST parameters sent by a user.
rails paperclip mysql does not save files
You have overridden the name
attribute of the file_field
, which has broken Rails naming conventions. If you remove the name
attribute, Rails will be able to pass the file details uploaded in the :cv
field to the database.
Change the file_field
to this:
<%= f.file_field :cv, class: 'form-control' %>
If you have Javascript attached to the form, and need to refer to the file_field
element, you can use this, instead:
<%= f.file_field :cv, id: "cv", class: "form-control" %>
Related Topics
Rails Object#Blank? VS. String#Empty? Confusion
Initializing a Hash with Empty Array Unexpected Behaviour
Chromedriver Devtools Port Number Error
Ruby: Wait for All Threads Completed Using Join and Threadswait.All_Waits - What the Difference
Where Does Rails Store Data Created by Saving Activerecord Objects During Tests
Rmagick How to Convert Image in Memory
How to Get a List from a Ruby Enumerable
Rails: Uninitialized Constant Just Happen on Production Server
Custom To_Yaml and Domain_Type
System New Line Separator in Ruby
How Can Same Program in Ruby Accept Input from User as Well as Command Line Arguments
Bundler::Rubyversionmismatch: Your Ruby Version Is 1.9.3, But Your Gemfile Specified 2.0.0
Ruby on Rails - Paperclip Not Saving to Database
How to Combination/Permutation in Ruby
How to Reinstall Ruby with Readline Support
Jekyll Plugin Not Work on Github
Error: Error Installing JSON: Error: Failed to Build Gem Native Extension
Devise Rendering Default Views from Gem Instead of Generated Ones