I just followed railscast 134 to install paperclip into my rails 3.0.1 app. I did everything necessary but when I select a file and press submit on my form, it reloads the page and says "no file chosen"
This is in my events model:
has_attached_file :photo, :styles => { :small => "150x150>" },
:url => "/assets/events/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/events/:id/:style/:basename.:extension"
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg',
'image/png',
'image/jpg']
and my form:
<h1>Edit Event</h1>
<%= form_for #event, :html => { :multipart => true } do |f| %>
<div class="actions">
<%= f.label :name %><br />
<%= f.text_field :name %><br /><br />
<%= f.label :description %><br />
<%= f.text_field :description %><br /><br />
<%= f.label :event_date %><br />
<%= text_field_tag :event_date, #event.event_date.try(:strftime, "%m/%d/%Y at %I:%M%p"), :size=>30 %><br /><br />
<%= f.label :location %><br />
<%= f.text_field :location %><br /><br />
<%= f.file_field :photo %><br /><br />
<%= f.submit "Update" %>
</div>
<% end %>
can't quite figure it out. thanks!
EDIT: some paperclip code in my event model:
has_attached_file :photo, :styles => { :small => "150x150>" },
:url => "/assets/events/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/events/:id/:style/:basename.:extension"
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg',
'image/png',
'image/jpg']
and some debug output:
{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"hScfg9tx/4gBOHrtg+u8MB+QeUWS1sKtlcbnzK8YmRI=", "event"=>{"name"=>"Park", "description"=>"Swimming", "location"=>"Golden Gate Park", "photo"=>#<File:/var/folders/lm/5svv8x4s4d50yhw_h05qz7mm0000gn/T/RackMultipart20111023-6938-8xsv50>}, "event_date"=>"10/10/2011 at 02:00PM", "commit"=>"Update", "action"=>"update", "controller"=>"events", "id"=>"3"}
Pretty sure that's normal behavior for a file_field, It only displays the file name when you select it to upload, it doesn't show the existing file saved in the Database.
If you want to display and access uploaded content try something like this in your view:
<%= link_to(image_tag(#event.photo.url(:small)), #event.photo.url) %>
Hope this helps
Related
I started working on a rails app
i want to add multiple image to my model.
model/product.rb
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :comments , dependent: :destroy
has_many :product_images, :dependent => :destroy
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].nil? }
end
model/product_image.rb
class ProductImage < ActiveRecord::Base
belongs_to:product
has_attached_file :image ,:styles => {
:thumb => ['100x100#', :jpg, :quality => 70],
:preview => ['480x480#', :jpg, :quality => 70],
:large => ['600>', :jpg, :quality => 70],
:retina => ['1200>', :jpg, :quality => 30]},
:path => ':rails_root/public/system/:id.:extension',
:convert_options => {
:thumb => '-set colorspace sRGB -strip',
:preview => '-set colorspace sRGB -strip',
:large => '-set colorspace sRGB -strip',
:retina => '-set colorspace sRGB -strip -sharpen 0x0.5'
}
validates_attachment_presence :image
validates_attachment_size :image , :less_than => 10.megabytes
validates_attachment_content_type :image , :content_type =>['image/jpeg','image/jpg','image/png']
validates :image, presence: true
end
My product form is as follows
_form.html.erb
<%= form_for #product, url: products_path, :html => {:multipart => true,:class => " form-horizontal center"} do |f| %>
<div class="field">
<%= f.label :name,class:"control-label" %>
<%= f.text_field :name,class:"form-control" %>
</div>
<div class="field">
<%= f.label :price,class:"control-label" %>
<%= f.number_field :price,class:"form-control"%>
</div>
<div class="field">
<%= f.label :description,class:"control-label" %>
<%= f.text_area :description,class:"form-control" %>
</div>
<div class="field">
<%= f.label :reason,class:"control-label" %>
<%= f.text_area :reason,class:"form-control" %>
</div><br/>
<div>
<%= f.label :status,class:"control-label col-md-1" %>
<div class="col-md-4">
<%= f.select :status, [["available"], ["sold"]], {}, {class: "form-control"} %>
</div>
<%= f.label :category_id,class:"control-label col-md-1" %>
<div class="col-md-4">
<%= f.collection_select :category_id, Category.all, :id, :name,{ :prompt => "Category", :multiple => true},{class: "form-control"} %>
</div>
<% fields_for :product_images do |builder| %>
<h1>
<%= builder.label :caption, "Image Caption" %>
<%= builder.text_field :caption %>
</h1>
<p>
<%= builder.label :image, "Image File" %>
<%= builder.file_field :image %>
</p>
<% end %>
</span>
</div>
<br/>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-default btn-primary" %>
</div><br/>
<% end %>
in my form i can see everything except labels mentioned in fileds_for tag which is used for multiple image upload.
Can somebody help me with this
I got my problem solved but can you help me how to use this to have multiple image upload.since i'm having for loop for images im have more labels in form and i don't want that.i want to one image selection input and i need to upload more than one image through there.help me with showing images also once product is saved
Since the fields_for method produces output, you need the equals-sign form of the opening erb tag:
<%= fields_for :product_images do |builder| %>
This answer has some nice coverage of the variations:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
Since you're using accepts_nested_attributes_for, you probably also want to use your parent form builder when calling fields_for, unless you're handling the product_images values manually.
<%= f.fields_for :product_images do |builder| %>
For more information on Erubis, the flavor of erb that Rails uses:
http://www.kuwata-lab.com/erubis
You have to write
<%= f.fields_for :product_images do |builder| %>
I'm trying to read the exif data from jpeg images that I upload with paperclip using the exifr gem and save focal length, etc to attributes but I can't seem to do it. I'm a rails beginner and would be most appreciative if anyone could assist.
my photo.rb model:
class Photo < ActiveRecord::Base
attr_accessible :date, :exif, :name, :photo
has_attached_file :photo, :styles => { :small => "200x200#" , :medium => "1280x720#" },
:url => "/assets/photos/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/photos/:id/:style/:basename.:extension"
after_photo_post_process :load_exif
validates_attachment_presence :photo
validates_attachment_content_type :photo, :content_type => ['image/jpeg']
def load_exif
exif = EXIFR::JPEG.new(photo.queued_for_write[:original])
return if exif.nil? or not exif.exif?
self.exposure = exif.exposure_time.to_s
self.f_stop = exif.f_number.to_f.to_s
self.focal_length = exif.focal_length.to_f.round.to_s
self.iso = exif.iso_speed_ratings
self.date = exif.date_time.to_date
rescue
false
end
end
my form (using SimpleForm):
<%= simple_form_for #photo, :html => {:multipart => true} do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.file_field :photo, :label => 'Attach photo' %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
show.html.erb with fields I'm trying to show:
<p>
<b>Name:</b>
<%= #photo.name %>
</p>
<p>
<b>Date:</b>
<%= #photo.date %>
</p>
<p>
<b>Exposure:</b>
<%= #photo.exposure %>
</p>
I'm probably missing something stupid so I apologize if that's so.
Try changing the first line in your load_exif method to:
exif = EXIFR::JPEG.new(photo.queued_for_write[:original].path)
Without .path on the end, I don't believe it's returning the file path.
I'm using accepts_nested_attributes_for in one of my Rails app. i have three models :album has_many :photo and each :photo has_and_belongs_to_many :tag. User can add more photo in album using paperclip and jquery.multifile.js. every thing working fine but the main problem is the hash is not properly create.
album => {:name => "", :body => "", :photos_attributes => {"tags_attributes" => {:name => "abc"}, "photo" => {"file" => "tempfile"}}}
but i need.
album => {:name => "", :body => "", :photos_attributes => "photo" => {"file" => "tempfile", "tags_attributes" => {:name => "abc"}}}}
My view is: please consider the gallery => album and attachment => photo
<%= form_for(#gallery, :html => {:multipart => true}) do |f| %>
<div id = "gallery_name_formField">
<%= f.label "Gallery Name:" %><br />
<%= f.text_field :name, :name => "gallery[name]" %><br />
</div>
<div id = "gallery_body_formField">
<%= f.label "Gallery Description:" %><br />
<%= f.text_area :body, :name => "gallery[body]" %><br />
</div>
<%= f.fields_for :attachments do |af| %>
<div id = "gallery_file_formField">
<%= af.label "Select The Attachment:" %><br />
<%= af.file_field(:attachment, :name => "gallery[attachments_attributes][][attachment]")%>
</div>
<div id="tags">
<%= af.fields_for :tags do |tf| %>
<%= tf.label :name, "Tag:" %><br />
<%= tf.text_field :name, :id => "mySingleField_0", :name => "gallery[attachments_attributes][][tags_attributes][][name]" %>
<% end %>
</div>
<% end %>
<div class="attachment_submit_formField">
<%= f.submit(:id => "create_gallery") %>
</div>
<% end %>
Have you tried to use nested form gem? Give it a try
Its very straightforward way to have multiple nested forms.
It works for me.
I have successfully installed Paperclip gem and uploaded a picture to my post, but in show action there isn't any picture, just alt text with image alt.
I am using ruby on rails 3.2.1
#post form:
<% create_url = {:url=>{:action=>"create"}} if #post.new_record? %>
<% form_for #post, :html => { :multipart => true } do |t| %>
<b><%= t.label :title, 'Virsraksts:' %></b><br />
<%= t.text_field :title %><br /><br />
<b><%= t.label :content, 'Teksts:' %></b><br />
<%= t.text_area :content %><br /><br />
<%= f.file_field :bildes %>
<div class="actions">
<%= t.submit %>
</div>
#post show:
<p><%= #post.content %></p>
<%= image_tag #post.bildes.url %>
<%= image_tag #post.bildes.url(:medium) %>
<%= image_tag #post.bildes.url(:thumb) %>
In my opinion #post model url and path is not right, but exactly what I don't know.
#post model:
class Post < ActiveRecord::Base
has_attached_file :bildes, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:url => ":rails_root/app/assets/bildes/:id/:style/:basename.:extension",
:path => ":rails_root/app/assets/bildes/:id/:style/:basename.:extension"
end
Try using the default url/path for the attached file by not specifying it in the model.
I've done the migration I have
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/missing/:style.png"
in my model, and im using
<%= form_for #user, :as => :user, :url => user_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<div class="actions">
<%= form.submit %>
</div>
To save the avatar uploaded and
<%= image_tag #user.avatar.url %>
<%= image_tag #user.avatar.url(:medium) %>
<%= image_tag #user.avatar.url(:thumb) %>
To display them all, but when I submit the new avatar it is not getting saved, I'm using
#user = User.create(params[:user])
in the create so it should be saving, what could be the problem?
<%= form_for #user, :validate => true, :html => { :multipart => true } do |f|%>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username, :disabled => 'disabled' %><br />
<%= f.label :full_name %><br />
<%= f.text_field :full_name%><br />
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<%= f.file_field :avatar %>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
ruby-1.9.2-p290 :002 > user = User.first
ruby-1.9.2-p290 :004 > user.avatar
=> /images/missing/original.png
Nothing is getting changed
avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>
You need to add :avatar to the attr_accessible list.
I assume that everything else for user is being saved properly?
The only thing that comes to my mind is, maybe you're using attr_accessible to protect from mass assignment and you forgot to add :avatar_file_name, :avatar_content_type, :avatar_file_size... to the whitelist?