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.
Related
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 Simple Form. I have a form for creating new items, and a form for editing existing ones. I also have two file fields for every item. Thing that bugs me is that file fields are displayed fine when creating new item, but then they are not generated at all when editing an existing item.
I had this perfectly working in Rails 3.0, now doesn't work on Rails 3.2.1.
The form:
<%= simple_form_for #item, :html => { :multipart => true } do |f| %>
<%= f.input :title, :input_html => { :maxlength => 35 } %>
<%= f.input :description, :input_html => { :maxlength => 450 } %>
<%= f.input :secure_details, :placeholder => "Serial numbers and other stuff that will remain private", :input_html => { :maxlength => 450 } %>
<%= f.association :bookmark, :collection => current_user.bookmarks(:order => :position), :include_blank => false %>
<%= f.input :warranty_until, :as => :string, :input_html => { :id =>'datepicker2' } %>
<div class="image_attachment">
<div class="attachment_text">
Update item photo<br />
<small>(will replace old one)</small>
</div>
<div class="attachment_button">
<% f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
</div>
</div>
<div class="image_attachment">
<div class="attachment_text">
Update receipt<br />
<small>(will replace old one)</small>
</div>
<div class="attachment_button">
<% f.fields_for :receipts do |receipt| %>
<%= receipt.file_field :photo %>
<% end %>
</div>
</div>
<%= f.input :public, :label => "My friends can see this item", :input_html => { :class => "right" } %>
<%= f.input :giveaway, :label => "Mark as giveaway", :input_html => { :class => "right" } %>
<div class="margin_r margin_t">
<%= f.button :submit, :class => 'small_button white right' %>
</div>
<% end %>
Basically this part of code doesn't work:
<div class="attachment_button">
<% f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
</div>
The generated HTML is just empty div.
The very same code works when creating a new item, but doesn't work when editing existing one.
Both Assets and Receipts are using Paperclip for storing images. Here is a code for Asset class:
class Asset < ActiveRecord::Base
belongs_to :item
has_attached_file :photo,
:styles => {
:thumb => "80x80#",
:small => "150x150>" }
validates_attachment_size :photo, :less_than => 550.kilobytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
end
Maybe you forgot add this line of code in your Item model:
accepts_nested_attributes_for :receipts, :allow_destroy => true
accepts_nested_attributes_for :assets, :allow_destroy => true
And add '=':
<%= f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
<%= f.fields_for :receipts do |receipt| %>
<%= receipt.file_field :photo %>
<% end %>
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?
I have a form (snippet)
<% form_for(#transfer, :html => {:multipart => true}) do |f| %>
<p>
<%= f.label :source %><br />
<%= f.text_field :source %>
</p>
<p>
<%= f.label :destination %><br />
<%= f.text_field :destination %>
</p>
<% fields_for :upload do |u| %>
<p>
<%= u.label :upload %><br />
<%= u.text_field :upload %>
</p>
<% end %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', transfers_path %>
So now in my transfers controller I can do:
#transfer = Transfer.new(params[:transfer])
#upload = Upload.find_or_create_by_md5(params[:upload])
I am able to post to a single form with XML by simply changing the params to XML like
<transfer>
<source>foo</source>
<destination>bar</destination>
</transfer>
or
<upload>
<upload>baz</upload>
</upload>
But I cannot figure out how to combine them under the same XML root
Assuming you're using Rails 2.3.x, you might want to look into adding accepts_nested_attributes_for to your Transfer model. See what-s-new-in-edge-rails-nested-attributes.
Well I haven't been able to figure out how to do this for XML, so for now I've had to settle for doing it with REST. I came across the RestClient library and looking through the source, figured out you could do nested params like this:
RestClient.post( url,
{
:transfer => {
:path => '/foo/bar',
:owner => 'that_guy',
:group => 'those_guys'
},
:upload => {
:file => File.new(path)
}
})
Send a note to the author about documenting the functionality here
Hallo
rails version 2.3.5
I'm learning rails and I run into a problem.
I'm doing some nesting forms from the railscasts tutorials. I changed the text area into a data field to upload photos and everything is working.
Now i have to display the uploaded pictures and i simply can't do it. I Tried everything I could find on the net but nothing worked.
PROBLEM
I have the Article controller which handles the article CRUD.
inside the article new form there is nested a form for uploading images.
article controller
def code_image
#image_data = Photo.find(params[:id])
#image = #image_data.binary_data
send_data(#image, :type => #image_data.content_type,
:filename => #image_data.filename,
:disposition => 'inline')
end
photo model
def image_file=(input_data)
self.filename = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.binary_data = input_data.read
end
articles/show.html.erb
<%=h #article.title %>
<%=h #article.body %>
<% for photos in #article.photos %>
<%= image_tag(url_for({:action => 'code_image',
:id => #article.photos.id})) -%>
<% end %>
articles/_formnew.html.erb
<% form_for (:article, #article,
:url => {:action=>'create'}, :html=>
{:multipart=>true}) do |f| %>
<%= f.label :title %><br />
<%= f.text_field :title %><br /><br />
<%= f.label :body %><br />
<%= f.text_area :body, :style => 'width: 600px;' %><br /><br />
<% f.fields_for :photos do |builder|%>
<%= builder.label :content, "Photo"%><br />
<%= builder.file_field :image_file %><br />
<% end %>
<br />
<%= f.submit "Create" %>
Thanks
Old question I know, but if somebody stumbles across it, this page helped me:
http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
Also, to display the image, you can just use:
image_tag [model].[paperclip_attachment].url
eg:image_tag trip_image.photo.url