snippet from my model:
attr_accessible :package1_file_name, :package2_file_name
has_attached_file :package1
has_attached_file :package2
from my _form (simplified version):
<%= form_for(#submission, :html => { :multipart => true, :id => "fileupload" }) do |f| %>
<%= f.file_field :package1%>
<%= f.file_field :package2%>
<% end %>
The problem is that paperclip will insert two separate entries for each file in the database.
However, I want it to insert into only one entries since I have two separate fields in my table: package1_file_name, package2_file_name.
Is there a way to achieve this?
Thank you!
For Christian Varga:
Maybe I shouldn't simplified the code in my original question, but my view actually look like this after using the jQuery file upload plugin:
<%= f.fields_for :uploads do |upload| %>
<div class="row fileupload-buttonbar">
<!-- The first upload field -->
<span class="btn btn-success fileinput-button">
<%= upload.file_field :package1 %>
</span>
<!-- The second upload field -->
<span class="btn btn-success fileinput-button">
<%= upload.file_field :package2 %>
</span>
</div>
<% end %>
Where upload is a child model of the current model
I am not sure whether the multipart makes the two file fields act like separate attachment, but I am trying to merge those two attachments into one.
Ok, so I've done a bit of research and I'm still unable to replicate your issue. I built a test application with that code, and it only inserts a single entry into the database.
Create Project (terminal)
rails new paperclip-test
cd paperclip-test
echo "gem 'paperclip'" >> Gemfile
bundle
rails generate scaffold submission
rails generate paperclip submission package1 package2
rake db:migrate
Update Model (submission.rb)
attr_accessible :package1, :package2
has_attached_file :package1, :styles => { :medium => "300x300>", :thumb => "100x100>" }
has_attached_file :package2, :styles => { :medium => "300x300>", :thumb => "100x100>" }
Update controller (submissions_controller.rb)
def create
# #submission = Submission.new(params[:submission])
#submission = Submission.create(params[:submission])
end
Update form (_form.html.erb)
<%= f.file_field :package1 %>
<%= f.file_field :package2 %>
Update view (show.html.erb)
<%= image_tag #submission.package1.url(:medium) %>
<%= image_tag #submission.package2.url(:medium) %>
Run app & create new submission
Go back to console:
sqlite3 db/development.sqlite3
select * from submissions;
Result:
1|2013-02-21 21:16:38.898602|2013-02-21 21:16:38.898602|image_1.jpg|image/jpeg|54231|2013-02-21 21:16:38.419947|image_2.jpg|image/jpeg|61766|2013-02-21 21:16:38.658720
Paperclip instructions from https://github.com/thoughtbot/paperclip#quick-start
Related
I have a demand,used rails 4 to upload the file,and i need save the file content into a database,file does not need to save.Now have no idea.I tried many way but failed to solve.Hope to get your help,thx.
You can use gem like paperclip(https://github.com/thoughtbot/paperclippaperclip) or carrierwave as a sample model it will be like this
class User < ActiveRecord::Base
has_attached_file :avatar,
:storage => :database,
:styles => { :thumb => "75x75>", :small => "150x150>" },
:url => '/:class/:id/:attachment?style=:style'
end
in storage you can specify the kind of saving you want
You can create a multipart form object. Please check the example below,
HTML View
<% form_for(:uploaded_file, #feed, :html=> {:multipart=>true}) do |f| %>
<p>
<%= f.label :uploaded_file, 'Upload your file.' %><br />
<%= f.file_field :uploaded_file %>
</p>
<p><%= f.submit 'upload' %></p>
<% end %>
Controller Action
file_data = params[:uploaded_file]
if file_data.respond_to?(:read)
xml_contents = file_data.read
elsif file_data.respond_to?(:path)
xml_contents = File.read(file_data.path)
else
logger.error "Bad file_data: #{file_data.class.name}: # {file_data.inspect}"
end
I am getting an error when trying to upload images to my (listings) using the paperclip gem. The error that the browser outputs is: 1 error prohibited this listing from being saved: Image has contents that are not what they are reported to be **As a note, image magic has been successfully installed on my computer and there are no issues there
my listing.rb file
class Listing < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
my gemfile
gem "paperclip", "~> 4.3"
my listings_controller
def listing_params
params.require(:listing).permit(:name, :description, :price, :image)
end
end
and finally my form
<%= form_for #listing, :html => { :multipart => true } do |f| %>
...
...
<div class="form-group">
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
It sounds like you need to include file on your system.
If you're using Windows, you need to download file from this URL, install it on your hard drive and then add it to your PATH environment var:
Click "Start"
On "Computer", right-click and select "Properties"
In Properties, select "Advanced System Settings"
Click the "Environment Variables" button
Locate the "PATH" var - at the end, add the path to your newly installed file.exe (typically C:\Program Files (x86)\GnuWin32\bin)
Restart any CMD shells you have open & see if it works
You're probably trying to attach a file that is not properly recognised as an image, or one that has an image extension and has different content (like a PDF, for example).
Some workarounds are discussed here: https://github.com/thoughtbot/paperclip/issues/1924
It might help to check the log file as well - it should tell you what Paperclip thinks the type of the attachment is.
I'm working with Ruby on Rails to develop an application and I'm really struggling with the following issue.
I have setup Cloudinary http://cloudinary.com/documentation/rails_integration and also Attachinary https://github.com/assembler/attachinary for users to upload a profile image using the rails application, I also want to be able to load these images in the Rails application to show the user's profile image.
I have setup the following code in the user.rb Model file
has_attachment :avatar, :accept => [:jpg, :png, :gif]
The edit page for User's comes under the Devise view in my application - edit.html.erb. The following form is what I'm using to upload User avatars/profile images:
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :post }) do |f| %>
<%= cl_image_upload_tag(:avatar) %>
<%= f.button :submit %>
<% end %>
In the same View file I'm using the following code to retrieve the profile image:
<div class="profile-image" align="center">
<% if #user.avatar.present? %>
<img src="<%= cloudinary_url(#user.avatar.path) %>" width="180px" height="180px" alt=""/>
<% else %>
<img src="<%= asset_path "person.png" %>" width="180px" height="180px" alt=""/>
<% end %>
</div>
Upon the image being uploaded to Cloudinary it is given a random file name, for example: ecwc8x4ult960jzk8dp0
Am I uploading the image correctly and how do I retrieve them?
In the form try: <%= user.attachinary_file_field :avatar %>.
You should be able to retrieve them through the model like this: <%= image_tag user.avatar.url if user.avatar? %>, or like it's said in the Attachinary's documentation: <%= cl_image_tag(#user.avatar.path, { size: '180x180', crop: :thumb, gravity: :face }) %>. Omit the <img> tag and I suggest to not use inline css.
You could have 400 error for serving the images over HTTP instead of HTTPS. To fix that you should add secure: true to the cloudinary.yml or add :secure => true to the image tag.
I can't write comments... Are there any errors, could you post error logs?
Hi I'm currently making an app for users to upload pics. I'm trying to add Amazon S3 in preparation to deploy my app. I'm not getting any error, but after trying to set up Amazon S3 my pics aren't uploading properly. The pics are uploading properly onto Amazon, not in my database so that when I upload the pics and try to loop through them in the views to show them, nothing shows. Can anyone assist me?
Just as a sidenote, I'm using Paperclip + jQuery file upload (for multiple pic uploads) + Amazon S3 + a s3_direct_upload gem (https://github.com/WayneHoover/s3_direct_upload)
config routes (notice that pics is nested in albums and users) no issue here
Pholder::Application.routes.draw do
resources :users do
resources :friends
resources :albums do
resources :photos
end
end
end
users/show.html.erb (the page that is supposed to show my uploaded pics, no new pics are showing up even after my uploads)
<% #user.albums.each do |album| %>
<div class="albumthumb">
<%= link_to image_tag(!album.photos.empty? ? album.photos.first.avatar.url(:medium) : ("questionmark.png"), :size => "150x150"), user_album_path(#user, album) %>
<br>
<%= album.name %>
</div>
<% end %>
photos/new.html.erb (issue here as well)
#my new uploader that works with amazon s3, but doesn't seem to be storing the pics in my database
<%= s3_uploader_form post: user_album_photos_path, as: "photo[avatar]", id: "myS3Uploader" do %>
<%= file_field_tag :file, multiple: true %>
<% end %>
#this is my old upload form that worked fine when uploading pics to my local public directory.
#
#<% provide(:title, "Upload pictures") %>
#
#<%= form_for([#user, #album, #photo], :html => { :multipart => true }) do |f| %>
#
#<%= f.file_field :avatar, multiple: true, name: "photo[avatar]" %>
<div id="pics">
<%= render :partial => "photo", :collection => #photos %>
</div>
<script id="template-upload" type="text/x-tmpl">
<div class="upload">
{%=o.name%}
<div class="progress"><div class="bar" style="width: 0%"></div></div>
</div>
</script>
Here's my code on github: https://github.com/EdmundMai/railstest/blob/master/app/views/photos/new.html.erb
Let me know if you need me to upload any more code. Please help!
JQuery file upload just uploads the files to Amazon S3: it doesn't add the required Paperclip records to the database. To do that, you need to do a few more steps. See the jquery-fileupload-rails-paperclip-example or jQuery File Upload v4 for Rails 3.
I'm using paperclip to upload images to the site but when I do, it shows the image placeholder with a question mark, not the image itself. What Am I doing wrong?
In the model:
attr_accessor :photo_file_name
attr_accessor :photo_content_type
attr_accessor :photo_file_size
attr_accessor :photo_updated_at
attr_accessible :photo
#paperclip-------------------------------
has_attached_file :photo,
:url => "/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension"
In the view:
<p id="notice"><%= notice %></p>
<div id="container">
<div id="content">
<%= image_tag #post.photo.url %>
<p>
<b>Title:</b>
<%= #post.title %>
and in the form I have:
<div class="field">
<%= post_form.file_field :photo %>
</div>
Get rid of all the accessor stuff and make sure your form returns something like:
params[:post][:photo]
Typically via:
form_for #post do |f|
f.file_field :photo
end
Then, in your posts controller, you can update or otherwise save a particular post with a :photo attached. Display the photo by simply tagging it image_tag #post.photo, Paperclip can do the rest.
I highly recommend removing your :url and :path options and just use the default for now, which is fine for most applications. It includes the system/ directory that is symlinked as shared by default under Capistrano, which is nice.
Did you check whether the image file is stored :rails_root/public/:attachment/:id/:style/ yet?
Right click to the image place holder to check the url of the image, is it right?