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?
Related
I have a form for a blog, and I would like to have two field for images. One image being the cover (in Show) and another image will serve as a preview (in index).
My form looks as follow:
<%= semantic_form_for #blog, :html => { :multipart => true } do |f| %>
<%= t :Choose_File_for_cover %> <%= f.file_field :image_path, id: "avatar-upload2", required: true %>
<img id="img_prev3" width="100%" height=200 src="#" alt="your image" class="img-thumbnail hidden"/>
<%= t :Choose_File_for_homepage %> <%= f.file_field :homepagepic, id: "avatar-upload3", required: true %>
<%= f.hidden_field :image_path_cache %>
<%= f.hidden_field :homepagepic_cache %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
My model looks like:
class Blog < ApplicationRecord
belongs_to :user
acts_as_taggable
mount_uploader :image_path, BlogUploader
mount_uploader :homepagepic, BlogcoverUploader
end
It works well when I only have the image_path (the cover), but when I add a new field for homepagepic, i get a ROLLBACK at validation.
Can someone help me on how to select files through two separate fields on the same form please.
Thank you
The code you've provided is very sparse and it would be helpful to see a little bit more (e.g. the controller and the uploader).
I can, however, hazard a guess: image_path is an existing helper method provided by Rails (see https://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html#method-i-image_path). I have absolutely no idea what happens when you use this as a name for a form field. It could also be because you declare your submit button to be an input (I've only ever seen and used as: :button for f.action :submit).
So overall, I would pick the following approach:
rename your upload fields to cover_image and the other one to preview_image (that's what you've described in your posts as their respective purpose, so you should name them accordingly)
change the submit to a button and remove all the noise from your template and start with the bare minimum: the two upload fields and nothing else (see sample code below – note that I haven't tested it but it should work or be very close to working)
after that works, start adding back the noise (i.e. the translations, the cache fields etc.)
Test that it still works after every step. If you can write a Capybara test, do that – otherwise test it manually.
If you have questions, feel free to ask.
<%= semantic_form_for #blog, html: { multipart: true } do |f| %>
<%= f.file_field :cover_image %>
<%= f.file_field :preview_image %>
<%= f.actions do %>
<%= f.action :submit, as: :button %>
<% end %>
<% end %>
class Blog < ApplicationRecord
belongs_to :user
acts_as_taggable
mount_uploader :preview_image, BlogUploader
mount_uploader :cover_image, BlogcoverUploader
end
As the previous poster said it's hard to debug your code without all the pieces to the puzzle. A ROLLBACK is happening because one or more validations failed.
Any time you have a ROLLBACK you can add a ! to the create or update method being called on the object being rolled back and ActiveRecord will throw an error telling you why the ROLLBACK happened instead of failing gracefully.
Once you know why your object isn't persisting you can check the params of the controller action that form is submitting to. Perhaps you forgot to whitelist a param via strong params?
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?
I wanted to try out Carrierwave_direct with my app so I decided to follow the Railscast episode. http://railscasts.com/episodes/383-uploading-to-amazon-s3
I am using Carrierwave for a variety of uploads including Images, Videos, and Songs. Right now though, I'm only testing the video to see how I like it. Right now, the video's are getting uploaded to my Amazon bucket, but the file (string) isn't getting saved to my database and not rendering on the page. Here is where I am right now...
Rails v 4.0.1
Ruby v 2.1.1
Video Uploader
class VideoUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
# I haven't added any resizing or anything yet
end
Video Model (video.rb)
mount_uploader :video, VideoUploader # In the database, the column is video:string
...
...
Page to upload file, before being redirected to form
def home
#uploader = Video.new.video
#uploader.success_action_redirect = new_video_url
end
Videos controller
def new
#video = Video.new(key: params[:key])
end
def create
#video = current_user.videos.create(video_params)
...
end
private
def video_params
params.require(:video).permit(:type, :title, :description, :video)
end
home.html.erb (where the video upload is BEFORE it successfully redirects you to the rest of the form)
<%= direct_upload_form_for #uploader do |f| %>
<p><%= f.file_field :video %></p>
<p><%= f.submit "Upload Video" %></p>
<% end %>
videos/_form.html.erb (the rest of the video form)
<%= form_for(#video, html: { :class => "full-form form-vertical" }) do |f| %>
<fieldset>
<%= render "shared/error_messages", object: f.object %>
<%= f.hidden_field :key %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, :class => "form-control" %>
</div>
<%= f.submit %>
</fieldset>
<% end %>
My amazon is configured properly, so I won't provide my carrierwave.rb initializer file.
So to reiterate, what's happening is on the first page, I will upload the video which will redirect me to the form. If I inspect the form, I can see that the :key has the proper value in it. But, if I put a presence validator on the model, the form won't submit because the video will be blank. And if there is no validation, the form will submit, amazon will upload it to my bucket, but (1) the video file name/string won't be stored in my db and (2) it won't render on the page, because it's empty.
Thanks for taking a look. Cheers.
#video_params needs to permit :key, in your video controller.
private
def video_params
params.require(:video).permit(:type, :title, :description, :video, :key)
end
You brought the :key all the way down, set it in #new, kept it with the hidden_field, and then lost it on the strong params.
The :key needs to get back to the VideoUploader object which is mounted under Video as :video, this is what allows your Video class to accept the :key attribute.
Video Model (video.rb)
mount_uploader :video, VideoUploader # In the database, the column is video:string
...
I was able to upload an image with paperclip.
I am now trying to show that image
<%= image_tag #profile.image.url %>
this renders
<img alt="Missing" src="/images/original/missing.png?1379966496">
And nothing is been displayed.
What am I doing wrong ?
In the model for profile be sure that you added:
attr_accessible :image
Paperclip will upload the file, but you need to whitelist it for it to be used in the view
Are you trying to edit this in a form? I had the same problem, but by having the following code in my
app/views/product(your resource name)/_form.html.erb
file fixed it:
<div class="field">
<%= f.label :avatar%><br>
<%= f.file_field :avatar %>
</div>
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