I have a rails app that consists of a CMS system that I use in order to enter sights from a city to my database. I am using paperclip to upload images to amazon s3. All is working fine. Now I want my json files that an ios app will use to include the urls of the images uploaded in s3. I have seen some answers here but I cannot seem to make my code work. What I have is this..
place model
attr_accessible :assets_attributes, :asset
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
def avatar_url
assets.map(&:asset_url)
end
asset model
class Asset < ActiveRecord::Base
attr_accessible :asset_content_type, :asset_file_name, :asset_file_size, :asset_updated_at, :place_id, :asset
belongs_to :place
has_attached_file :asset
validates_attachment :asset, :presence => true,
:content_type => { :content_type => ['image/jpeg', 'image/png'] },
:size => { :in => 0..1.megabytes }
def asset_url
asset.url(:original)
end
end
view code
<%= f.fields_for :assets do |asset_fields| %>
<% if asset_fields.object.new_record? %>
<p>
<%= asset_fields.file_field :asset %>
</p>
<% end %>
<% end %>
<br/>
<%= f.fields_for :assets do |asset_fields| %>
<% unless asset_fields.object.new_record? %>
<%= link_to image_tag(asset_fields.object.asset.url(:original), :class => "style_image"), (asset_fields.object.asset.url(:original)) %>
<%= asset_fields.check_box :_destroy %>
<% end %>
<% end %>
places controller
def index
#places = Place.all
render :json => #places.to_json(:methods => [:avatar_url])
end
Can anyone please help me?
In reference to the SO question you linked to (How can I get url for paperclip image in to_json), there are certain elements you'll need in order to get the image to render correctly
The problem you have is Paperclip's image method is actually an ActiveRecord object, and therefore you cannot just render it in a JSON request without doing some other stuff
The _url Method
The most important part of the process is to define the "_url" method in your asset model. This basically calls the .url function of Paperclip, allowing JSON to create the desired URL of the image on the fly (The url of the image is not an ActiveRecord object, and can therefore be sent via JSON)
As per the referenced SO question, you should put this action in your model:
#app/models/asset.rb
def asset_url
asset.url(:medium)
end
Now when you render the JSON request in your controller, you can use this type of setup:
#app/controllers/places_controller.rb
render :json => #places.to_json(:methods => [:asset_url])
Because your asset model is an associate of places, this might not work straight away. However, it's definitely in the right direction, because I can remember doing this exact thing myself
The important thing to note here, is that you're actually passing the naked "URL" of the image through JSON, not the image object itself
Update
Here's an example from our video conference demo app we made:
#app/controllers/profiles_controller.rb
def update
#profile = User.find(current_user.id)
#profile.profile.update(upload_params)
respond_to do |format|
format.html { render :nothing => true }
format.js { render :partial => 'profiles/update.js' }
format.json { render :json => #profile.profile.as_json(:only => [:id, :avatar], :methods => [:avatar_url])
}
end
end
#app/models/profile.rb
def avatar_url
avatar.url(:original)
end
So for you, I'd try this:
def index
#places = Place.all
render :json => #places.assets.as_json(:only => [:id, :asset], :methods => [:asset_url])
end
You could also try something like this:
#app/models/profile.rb
def avatar_url
self.asset.avatar.url(:original)
end
Related
Im following this tutorial: https://www.devwalks.com/lets-build-instagram-in-rails-part-1/
To create a version of instagram. When I upload an image, add a caption and submit, it will redirect to the index page as expected but the data doesnt seem to have been saved. When I open the rails console and try to get the posts with Posts.first, it returns nil.
Controller:
class PostsController < ApplicationController
def index
end
def new
#post = Post.new
end
def create
#post =Post.create(post_params)
#post.save
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:image, :caption)
end
end
Model:
class Post < ActiveRecord::Base
validates :image, presence: true
has_attached_file :image, styles: { :medium => "640x"}
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
form:
<%= simple_form_for #post do |f| %>
<%= f.input :image %>
<%= f.input :caption %>
<%= f.button :submit %>
<% end %>
routes:
resources :posts
root 'posts#index'
Appreciate any ideas.
Thanks
I see a few problems here:
create will save so you don't need another #post.save.
create returns the new Post object, but you have to check if it has been saved successfully or not (via #post.persisted, or via if #post.save).
From 1 & 2, I believe your post was not saved, due to validation on image presence.
Now why it's happening? I guess your form has no multipart/form-data set that the image file was not submitted at all.
To add that to simple_form (paperclip README) :
<%= simple_form_for #post, html: { multipart: true } do |f| %>
I've used the following technique to successfully upload multiple files using paperclip (without using nested attributes)
Project.rb
has_many :photos, :dependent => :destroy
Photo.rb
has_attached_file :asset, :styles => { :medium => "300x300>" }
belongs_to :project
photos/new.html.erb
<%= form_for #photo, :html => { multipart: true } do |f| %>
<div>
<%= f.label :asset, 'Project Photos', class: 'label1' %>
<%= file_field(:photo, :asset, :multiple => true) %>
</div>
<div>
<%= f.submit "Add photos to project" %>
</div>
Photos_controller:
def create
#project = Project.find(params[:id])
#client = Client.find(params[:id])
params[:photo][:asset].each do |file|
#photo = #project.photos.build(:asset => file)
if
#photo.save.......
photos/show.html.erb
<div>
<% #project.photos.each do |p| %>
<%= image_tag(p.asset.url(:square)) %>
<%= check_box_tag 'destruction[]', p.id %>
<% end %>
Routes file:
resources :photos do
collection do
delete 'destroy_multiple'
end
end
I'm trying to create an array of id's based on checkboxes to be passed to the destroy_multiple action in the photos controller. params[:destruction] does yield the correct ids.
What would be the most efficient way of telling the destroy_multiple action to delete only those assets whose id's are in the destruction array?
Many thanks for your time.
destroy_all
Paperclip is just a link between the saved asset (image/video), and your ORM (in our case ActiveRecord).
This means that you should still be able to perform all the queries you want through your standard AR methods (destroy_all etc), with Paperclip removing the relevant assets as you require.
As such...
#config/routes.rb
resources :photos do
delete :destroy_multiple, action: :destroy, on: :collection
end
#app/controllers/photos_controller.rb
class PhotosController < ApplicationController
def destroy
ids = params[:destruction] || params[:id]
Photo.destroy_all ids if ids
end
end
Paperclip will handle the rest!
I am looking for a solution to give the user the ability to upload multiple images through one file_field. I have looked in to options such a Jquery File Upload and Uploadify but have yet to come across good examples with a working solution.
I already have multiple images setup,
has_attached_file :asset,
:styles => { :large => "640x480", :medium => "300x300", :thumb => "100x100" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:contributor_id/:listing_name/:filename"
Right now I am displaying 5 individual file_fields
def new
#listing = Listing.new
5.times {#listing.assets.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: #listing }
end
end
I would like to have
<%= f.file_field :asset, :multiple => true %>
That allows the user to select multiple files in their file browser. But how can I process these with a nested model? And get them to upload.
So there are a few issues here.
First, Paperclip's has_attached_file method isn't an association to many files. It looks like you're trying to build an "asset" as if it's a Rails association. All Paperclip does is put a couple of fields into your table to store some meta-data about the file and you get one attached file per declaration of has_attached_file. If you want to attach 5 files, you would need to do something like:
has_attached_file :asset1
has_attached_file :asset2
has_attached_file :asset3
has_attached_file :asset4
has_attached_file :asset5
OR, alternatively, you could create another model just to store the files. For example:
class Listing < ActiveRecord::Base
has_many :assets
end
class Asset < ActiveRecord::Base
belongs_to :listing
has_attached_file :picture
end
That way, you could have multiple assets attached to one listing (you didn't say what the original object was so I just called it "listing").
Second, there is no such thing as a multiple file upload in HTML (and, as such, the file_field method doesn't take a :multiple => true argument. You'll have to use something beyond Rails built-in form handling if you want multiple-file upload. Uploadify is a decent choice (that I've used before). There is a gem that will transform file fields to use uploadify (and will support the :multiple => true syntax that you want): https://github.com/mateomurphy/uploadify_rails3/wiki. However, I cannot vouch for how good it is.
My advice would be to start step-by-step. Uploading via Flash to Rails can be a complicated process that involves dealing with the CSRF meta-tag and other fields in your form. Start by making a form that allows a user to upload one file and stores it through Paperclip. Then maybe break the has_attached_file declaration into another model so that you can have 1 or many files associated with a model (as shown in the multi-model code block above). Then try adding Uploadify or another alternative. Ernie Miller has a decent tutorial on integrating Uploadify: http://erniemiller.org/2010/07/09/uploadify-and-rails-3/.
To start, remember that has_attached_file can only attach one file. When you try calling #listing.assets there is no "assets". There is an asset. You need to create a separate model yourself and use Rails' associations if you want multiple files.
Accepted answer says there is no such thing as a multiple file upload in HTML.
<%= f.file_field :files, multiple: true %>
This allows you to select multiple images and send them as an array.
If you have the relationship Dog has_many Images and Image has_attachment :file, do this to get multiple images to upload at once:
In your html.erb
<%= form_for #dog, html: { multipart: true } do |f| %>
<%= f.file_field :files, accept: 'image/png,image/jpeg,image/gif', multiple: true %>
<%= end %>
In your controller
def dog_params
params.require(:dog).permit files: []
end
In your Dog model
def files=(array = [])
array.each do |f|
images.create file: f
end
end
This is assuming you're already able to upload one image but want to upgrade to multiple images at once. Notice that wait time will increase.
To help reduce wait time, peep my post on this question related to speed uploading.
Here is a full example of multiple file uploads. Here a user has_many uploads. Each upload model has an avatar which represents the file attachment. Ultimately: we are creating many uploads when we create the user.
The Models
#models/user.rb
class User < ApplicationRecord
has_many :uploads
def files=(array_of_files = [])
array_of_files.each do |f|
uploads.build(avatar: f, user: self)
end
end
end
#models/upload.rb
class Upload < ApplicationRecord
belongs_to :user
has_attached_file :avatar
validates_attachment_content_type :avatar, :content_type => ["image/png"]
end
The form:
# views/users/_form.html.erb
<%= form_with(model: user, local: true) do |form| %>
...
<div class="field">
<%= form.file_field :files, multiple: true %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
The Controller
class UsersController < ApplicationController
before_action :set_user, only: [:show]
def show
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
redirect_to #user, notice: 'User was successfully created.'
end
end
private
def set_user
#user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name, files: [])
end
end
User#show
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #user.name %>
</p>
<h3>Uploads</h3>
<div>
<% #user.uploads.each do |upload| %>
<div>
<%= link_to upload.avatar.url do%>
<%= upload.avatar_file_name %>
<% end %>
</div>
<% end %>
</div>
<%= link_to 'Edit', edit_user_path(#user) %> |
<%= link_to 'Back', users_path %>
I am trying to create URL upload with paperclip.
I have followed this guide: http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/
The problem is that nothing gets uploaded when I use the image_url fields. I know my code isnt very dry therefor it would be nice if someone had, some tips to rewrite the code.
I have 2 attached images and therefor 2 image URLs.
My konkurrancers table:
photo_file_name varchar(255)
photo_content_type varchar(255)
photo_file_size int(11)
photo_updated_at datetime
photo2_file_name varchar(255)
photo2_content_type varchar(255)
photo2_file_size int(11)
photo2_updated_at datetime
image_remote_url varchar(255)
image_remote_url_2 varchar(255)
My konkurrancer model:
class Konkurrancer < ActiveRecord::Base
has_attached_file :photo,
:url => "/public/images/billeder/photo/:id/:basename.:extension",
:path => ":rails_root/public/images/billeder/photo/:id/:basename.:extension"
has_attached_file :photo2,
:url => "/public/images/billeder/photo2/:id/:basename.:extension",
:path => ":rails_root/public/images/billeder/photo2/:id/:basename.:extension"
before_validation :download_remote_image, :if => :image_url_provided?
before_validation :download_remote_image_2, :if => :image_url_2_provided?
validates_presence_of :image_remote_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'
validates_presence_of :image_remote_url_2, :if => :image_url_2_provided?, :message => 'is invalid or inaccessible'
private
def image_url_provided?
!self.image_url.blank?
end
def image_url_2_provided?
!self.image_url_2.blank?
end
def download_remote_image
self.photo = do_download_remote_image
self.image_remote_url = image_url
end
def download_remote_image_2
self.photo2 = do_download_remote_image_2
self.image_remote_url_2 = image_url_2
end
def do_download_remote_image
io = open(URI.parse(image_url))
def io.original_filename; base_uri.path.split('/').last; end
io.original_filename.blank? ? nil : io
rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
end
def do_download_remote_image_2
io = open(URI.parse(image_url_2))
def io.original_filename; base_uri.path.split('/').last; end
io.original_filename.blank? ? nil : io
rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
end
end
My controller create action:
def create
#konkurrancer = Konkurrancer.new(params[:konkurrancer])
respond_to do |format|
if #konkurrancer.save
format.html { redirect_to(:admin_konkurrancers, :notice => 'Konkurrancer was successfully created.') }
format.xml { render :xml => :admin_konkurrancers, :status => :created, :location => #konkurrancer }
else
format.html { render :action => "new" }
format.xml { render :xml => #konkurrancer.errors, :status => :unprocessable_entity }
end
end
end
My form:
<%= simple_form_for [:admin, #konkurrancer], :html => { :multipart => true } do |f| %>
<%= f.label :upload_125x125 %>
<%= f.file_field :photo, :label => '125x125', :style => 'width:250;' %>
<%= f.input :image_url_2, :label => 'URL 125x125', :style => 'width:250;' %>
<%= f.label :upload_460x60 %>
<%= f.file_field :photo2, :label => '460x58', :style => 'width:250;' %>
<%= f.button :submit, :value => 'Create konkurrence' %>
<% end %>
In the latest version of paperclip (pull request has been merged but i'm not sure about the release) paperclip > 3.1.3 (maybe 3.2 is upcoming; maybe 3.1.4) this is become even easier.
self.photo = URI.parse("http://something.com/blah/image.png")
The above should take care of download/tempfile stuff/filename and filecontent type.
Enjoy! :)
To fix the problem of an repetitive model, you'll want to instead create a separate class for your photos that stores a foreign key to the konkurrence:
class Photo < ActiveRecord::Base
has_attached_file ...
belongs_to :konkurrence
...
end
class Konkurrence < ActiveRecord::Base
has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos, :allow_destroy => true
...
end
Also, I think you're trying to download a remote image from a URL and then save this into Paperclip. Using open-uri (as I believe you already are), you can do this like so:
# open a tempfile using the last 14 chars of the filename
t = Tempfile.new(image_url.parameterize.slice(-14, 14))
t.write(open(image_url).read)
t.flush
t # return the File. You can then set the paperclip attribute to this File, eg, self.photo = t
This will save your URL as a temporary file which you can then pass on to Paperclip for regular processing.
I'm working on a website that allows people who run bed and breakfast businesses to post their accommodations.
I would like to require that they include a "profile image" of the accommodation when they post it, but I also want to give them the option to add more images later (this will be developed after).
I thought the best thing to do would be to use the Paperclip gem and have a Accommodation and a Photo in my application, the later belonging to the first as an association.
A new Photo record is created when they create an Accommodation. It has both id and accommodation_id attributes. However, the image is never uploaded and none of the Paperclip attributes get set (image_file_name: nil, image_content_type: nil, image_file_size: nil), so I get Paperclip's "missing" photo.
Any ideas on this one? It's been keeping me stuck for a few days now.
Accommodation
models/accommodation.rb
class Accommodation < ActiveRecord::Base
validates_presence_of :title, :description, :photo, :thing, :location
attr_accessible :title, :description, :thing, :borough, :location, :spaces, :price
has_one :photo
end
controllers/accommodation_controller.erb
class AccommodationsController < ApplicationController
before_filter :login_required, :only => {:new, :edit}
uses_tiny_mce ( :options => {
:theme => 'advanced',
:theme_advanced_toolbar_location => 'top',
:theme_advanced_toolbar_align => 'left',
:theme_advanced_buttons1 => 'bold,italic,underline,bullist,numlist,separator,undo,redo',
:theme_advanced_buttons2 => '',
:theme_advanced_buttons3 => ''
})
def index
#accommodations = Accommodation.all
end
def show
#accommodation = Accommodation.find(params[:id])
end
def new
#accommodation = Accommodation.new
end
def create
#accommodation = Accommodation.new(params[:accommodation])
#accommodation.photo = Photo.new(params[:photo])
#accommodation.user_id = current_user.id
if #accommodation.save
flash[:notice] = "Successfully created your accommodation."
render :action => 'show'
else
render :action => 'new'
end
end
def edit
#accommodation = Accommodation.find(params[:id])
end
def update
#accommodation = Accommodation.find(params[:id])
if #accommodation.update_attributes(params[:accommodation])
flash[:notice] = "Successfully updated accommodation."
render :action => 'show'
else
render :action => 'edit'
end
end
def destroy
#accommodation = Accommodation.find(params[:id])
#accommodation.destroy
flash[:notice] = "Successfully destroyed accommodation."
redirect_to :inkeep
end
end
views/accommodations/_form.html.erb
<%= form_for #accommodation, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
Title<br />
<%= f.text_field :title, :size => 60 %>
</p>
<p>
Description<br />
<%= f.text_area :description, :rows => 17, :cols => 75, :class => "mceEditor" %>
</p>
<p>
Photo<br />
<%= f.file_field :photo %>
</p>
[... snip ...]
<p><%= f.submit %></p>
<% end %>
Photo
The controller and views are still the same as when Rails generated them.
models/photo.erb
class Photo < ActiveRecord::Base
attr_accessible :image_file_name, :image_content_type, :image_file_size
belongs_to :accommodation
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "150x150>" }
end
To create an upload with paperclip, you need to use the name you provided for the has_attached_file line, on the model you defined it on. In your case, this will result in this view code:
<%= form_for #accommodation, :html => { :multipart => true } do |f| %>
<%= f.fields_for :photo do |photo_fields| %>
<p>
Photo<br />
<%= photo_fields.file_field :image %>
</p>
<% end %>
<% end %>
In the controller:
class AccommodationsController < ApplicationController
# also protect create and update actions!
before_filter :login_required, :only => [ :new, :create, :edit, :update ]
def new
# always make objects through their owner
#accommodation = current_user.accommodations.build
#accommodation.build_photo
end
def create
#accommodation = current_user.accommodations.build(params[:accommodation])
if #accommodation.save
# always redirect after successful save/update
redirect_to #accommodation
else
render :new
end
end
end
Tell Rails to handle the nested form:
class Accommodation
has_one :photo
accepts_nested_attributes :photo
attr_accessible :photo_attributes, :title, :description, :etc
end
And make sure to set the accessible attributes right in your photo model:
class Photo
attr_accessible :image # individual attributes such as image_file_name shouldn't be accessible
has_attached_file :image, :styles => "etc"
end
Be sure to watch your log files to spot things that are protected by attr_accessible, but still are in your form.