I've just started using Paperclip today and am having issues getting images to render. After some wrangling the photos are saving in the correct directory but there is a routing/rendering error:
ActionController::RoutingError (No route matches "/public/system/products/19/original/puppies-3.jpg")
However, the images are definitely saving in the correct directory. This is what's in my product model:
class Product < ActiveRecord::Base
validates :title, :presence => true
validates :description, :presence => true
validates :category, :presence => true
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
attr_accessible :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at
attr_accessible :title, :description, :category, :price
has_attached_file :photo, :styles => { :small => "150x150>", :large => "400x400>" },
:path => ":rails_root/public/system/products/:id/:style/:basename.:extension",
:url => "/system/products/:id/:style/:basename.:extension"
end
This is in my view:
<%= image_tag #product.photo.url %>
At the moment it's simply returning the image basename instead of the image itself, any thoughts? Products is available as a resource in routes.rb, but do I need to explicitly make photos available also somehow? I'm also fairly new to Rails so struggling a little bit...
Are you sure that product with that id has a image attached? I think some of your products dont have images attached but u still looking for them. Maybe try to set a default_url. Something like this:
has_attached_file :photo, :default_url => "/images/missing.png"
This will show that image set in default_url for the products what dont have images attached.
It's ok, I've fixed it. I had left a field in my form which was naming the photo_file_name from when I couldn't get the files to save and was trying to manually override it to get it to work (face-palm). It's amazing how much clarity comes from a few hours sleep.
Related
Hi everyone I am new with rails 3,I have a app where I want to associate a idea with a comment.
When I show an idea, in the bottom of the view I show a form to put new comment for this idea, and when a click to save a comment, I have to pass the idea_id, I create my model commment
belongs_to :user
belongs_to :idea
attr_accessible :description, :likes, :name, :user_id, :idea_id
in the view of show idea a put this
= render :partial => "comments/index", :collection => #idea.comments
= render :partial => "comments/form", :locals => {:comment=> #comment}
in the _form of the comment I include idea to obtain idea_id to save
= form_for [#idea, #comment] do |f|
and in my router I put this
resources :ideas do
member do
resources :comments
end
end
and now I obtain this error
undefined method `idea_comments_path'
any idea, anyone knows a document to explain better how to use member in rails!
You don't need member for nested resources:
resources :ideas do
resources :comments
end
http://guides.rubyonrails.org/routing.html
I came back to the relatively "old book" Head First rails, which was published for Rails 2.3.
Now, going back again through those samples and using Rails 3 I came up with some questions.
Let's say that I'm adapting the sample for coconut airways and instead of flights and seats, I have a project and tasks.
The page shows a project description and below a list of tasks associated to that project. so far so good. now below that there is a form to create new task. This task needs a Task object and the project_id. here is when things do not work as before.
if you want to do it like the old style you will type:
<%= render :partial => "new_task",
:locals => {:task => Task.new(#project.id)} %>
well, this is showing the mass-assign error.
Then I tried to pass both as parameter:
<%= render :partial => "new_task",
:locals => {:task => Task.new, :project_id => #project.id} %>
and assign it in the partial
<%= f.hidden_field :project_id, :value => project_id %>
any hint?
EDITED:
class Task < ActiveRecord::Base
belongs_to :project
attr_accessible :title
end
class Project < ActiveRecord::Base
has_many :tasks
attr_accessible :description, :title
end
If you change your model's attr_accessible you can include these assignments to be made. For more information about attr_accessible and mass assignment see: Ruby on Rails API
I am using paperclip to upload photos in my two rails app which share a single database. Now the problem is, if I upload a photo in app-a, for instance, paperclip gives me a url as:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-a/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Here is what I set up in my item model:
class Item < ActiveRecord::Base
attr_accessible :photo, :photo_url, :poll_id, :brand, :number_of_votes
has_attached_file :photo,
:styles => { :thumbnail => "100x100#",
:small => "150x150>",
:medium => "250x250>",
:large => "400x400>" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:url=>"/item_:id/created_at_:created_at/:style.jpg"
Paperclip.interpolates :created_at do |attachment, style|
attachment.instance.created_at
end
end
In the other app app-b, when I query the url with item.photo.url(:large), it gave me:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-b/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Therefore I got a wrong url.
Are there any ways I can do that by configuring paperclip? Thanks!
As described here : https://github.com/thoughtbot/paperclip#defaults , you can change the default config, in your case I guess you want to change the :local_root so it doesn't include the app name.
I'm trying to set up a polymorphic association for photo uploads which are processed using Carrierwave. I'm using Simple Form to build my forms. I feel like the association is correct so I'm wondering if my problem is just something with the form or controller.
Here are my associations:
property.rb:
class Property < ActiveRecord::Base
attr_accessible :image
...
has_many :image, :as => :attachable
...
end
unit.rb
class Unit < ActiveRecord::Base
attr_accessible :image
...
has_many :image, :as => :attachable
end
image.rb
class Image < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
mount_uploader :image, PhotoUploader
end
properties_controller.rb:
def edit
#property = Property.find params[:id]
#property.image.build if #property.image.empty?
end
def update
#property = Property.find params[:id]
if #property.update_attributes params[:property]
redirect_to admin_properties_path, :notice => 'The property has been successfully updated.'
else
render "edit"
end
end
Snippet from properties/_form.html.erb
<%= f.input :image, :label => 'Image:', :as => :file %>
Here is the error I get when submitting with an image attached:
undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x00000102291bb8>
And here are the params:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"lvB7EMdc7juip3gBZD3XhCLyiv1Vwq/hIFdb6f1MtIA=",
"property"=>{"name"=>"Delaware Woods",
"address"=>"",
"city"=>"",
"state"=>"",
"postal_code"=>"",
"description"=>"2 bedroom with large kitchen. Garage available",
"incentives"=>"",
"active"=>"1",
"feature_ids"=>[""],
"user_ids"=>[""],
"image"=>#<ActionDispatch::Http::UploadedFile:0x00000102291bb8 #original_filename="wallpaper-4331.jpg",
#content_type="image/jpeg",
#headers="Content-Disposition: form-data; name=\"property[image]\"; filename=\"wallpaper-4331.jpg\"\r\nContent-Type: image/jpeg\r\n",
#tempfile=#<File:/tmp/RackMultipart20120608-3102-13f3pyv>>},
"commit"=>"Update Property",
"id"=>"18"}
I'm looking everywhere for help on polymorphic associations and am getting nowhere. I've seen simple examples that look pretty straight forward. One thing I've noticed is that it seems like in a lot of the examples the has_many association in my case should be images and not image. However when I do that I get an error:
Can't mass-assign protected attributes: image
I've tried updating my form to use fields_for as I've seen in other blogs like so:
<%= f.input :image, :label => "Photo", :as => :file %>
<% f.simple_fields_for :images do |images_form| %>
<%= images_form.input :id, :as => :hidden %>
<%= images_form.input :attachable_id, :as => :hidden %>
<%= images_form.input :attachable_type, :as => :hidden %>
<%= images_form.input :image, :as => :file %>
<% end %>
All I know is I'm having a heck of a time getting this to work. I'm pretty new to Rails so even debugging it is difficult. It doesn't help that the debugger doesn't really work in 3.2 :(
Since your models have_many :images (it should be :images, not :image), you'll want to use nested_forms in your views. You should set up accepts_nested_attributes_for :images on the unit and property models and change the attr_accessible from :image to :image_attributes.
Check out http://railscasts.com/episodes/196-nested-model-form-part-1 for a good guide on getting going with it.
I've been struggling with this for hours. For some background, I have paperclip set up, keeping in mind that I may one day want to add multiple attachments. I followed Emerson's screencast to help me figure it out. (http://www.emersonlackey.com/article/paperclip-with-rails-3)
I now have this in my view, which shows what I want it to show. I had trouble for a long time because it was kicking up errors when a thumbnail didn't exist for some posts.
Anyway, wrote it, it's in my view, and I just think it's really ugly. I feel like I must be missing something. For one thing, I'm completely repeating myself on one line. Secondarily, I've got this code in my view. Is there something that I should be doing in my controller to help me keep my view cleaner?
Thanks a bunch!
<% if Asset.where(:piece_id => piece.id).first
my_asset = Asset.where(:piece_id => piece.id).first%>
<%= piece.id%>
<%= image_tag my_asset.asset.url(:thumb)%>
<% end%>
Because I haven't done anything to my controller to speak of, I'm leaving all of that code out. But here's what my models look like:
Assets
class Asset < ActiveRecord::Base
belongs_to :piece
has_attached_file :asset, :styles => {:large => ['700x700', :jpg], :medium => ['300x300>', :jpg], :thumb => ["100x100>", :jpg]}
end
Pieces
class Piece < ActiveRecord::Base
attr_accessible :assets_attributes,:name, :campaign_id,:election_date, :mail_date, :pdf_link, :photo_desc, :photo_stock, :killed, :format, :artist
belongs_to :client
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
validates :campaign_id, :presence => true
end
So your problem is that sometimes a Piece has a thumbnail and sometimes it doesn't, right?
I'd agree that your ERB solution smells bad. You could add a thumb_nail_url method to Piece:
def thumb_nail_url
asset = assets.first
asset ? asset.asset.url(:thumb) : nil
end
And then:
<% thumb_url = piece.thumb_nail_url %>
<% if thumb_url %>
<%= image_tag thumb_url %>
<% end %>
You could also wrap the above in a helper:
def piece_thumb_image_tag(piece)
thumb_url = piece.thumb_nail_url
thumb_url ? image_tag(thumb_url) : ''
end
and then:
<%= piece_thumb_image_tag piece %>