I am using paperclip in my rails app
my form is
<%= form_for #portfolio_photo, :url => portfolio_photo_uplaod_individual_profile_path(:profile_id => current_individual.profile.id), :method => :POST, :html => { :multipart => true } do |f| %>
<%= f.hidden_field :profile_id, :value => current_individual.profile.id %>
<%= file_field_tag :portfolio_photo, multiple: true %>
<%= f.submit "submit" %>
<% end %>
and controller action is
def portfolio_photo_uplaod
#portfolio_photo = IndividualPhoto.create(portfolio_photo_params)
if #portfolio_photo.save
redirect_to individual_profile_path(current_individual)
end
end
and the strong parameters are
def portfolio_photo_params
params.permit(:portfolio_photo, :profile_id)
end
individual_photo.rb
class IndividualPhoto < ActiveRecord::Base
belongs_to :profile
has_attached_file :portfolio_photo, :styles => { :medium => "300x300>" }
validates_attachment_content_type :portfolio_photo, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
profile.rb
has_many :individual_photos
i am able to save when i upload a single image but i am not able to save multiple images instead only one image is saving in the database when i upload multiple images
Please help !!
Here is the answer for your current system:
In your form:
<%= form_for #portfolio_photo, :url => portfolio_photo_uplaod_individual_profile_path(:profile_id => current_individual.profile.id), :method => :POST, :html => { :multipart => true } do |f| %>
<%= f.hidden_field :profile_id, :value => current_individual.profile.id %>
<%= file_field_tag 'portfolio_photos[]', multiple: true %>
<%= f.submit "submit" %>
<% end %>
In your controller:
def portfolio_photo_uplaod
portfolio_photo_params[:portfolio_photos].each do |photo|
IndividualPhoto.create(portfolio_photo: photo, profile_id: portfolio_photo_params[:profile_id])
end
redirect_to individual_profile_path(current_individual)
end
and the strong parameters:
def portfolio_photo_params
params.permit(:profile_id, :portfolio_photos => [])
end
With your current design, above solution should work but it's not the best approach yet, you have some better ways to achieve this e.g. using accepts_nested_attributes_for to update Profile's photos through Profile Controller.
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 would like to create an edit page for the below form. The problem is that when the user browses to the edit page the brand_name and name are pre-filled, but the image upload field shows 'no file chosen' even when an avatar exists for the 'style'. Please let me know if there is some way to remedy this. Thanks!
My Edit Form:
<%= simple_form_for #style, :html => { :class => 'form-horizontal' }, :remote => true do |m| %>
<%= m.input :brand_name, :label => 'Brand', :placeholder => 'Brand' %>
<%= m.input :name, :label => 'Style', :placeholder => 'Style' %>
<%= m.input :avatar, :label => "Image" %>
<div class="form-actions" style = "background:none">
<%= m.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', styles_path, :class => 'btn' %>
</div>
<% end %>
Just implemented this yesterday. Make a custom input in /app/inputs
class AvatarInput < SimpleForm::Inputs::FileInput
def input
out = '' # the output string we're going to build
# check if there's an uploaded file (eg: edit mode or form not saved)
if object.send("#{attribute_name}?")
# append preview image to output
# <%= image_tag #user.avatar.url(:thumb), :class => 'thumbnail', id: 'avatar' %>
out << template.image_tag(object.send(attribute_name).url(:thumb), :class => 'thumbnail', id: 'avatar')
end
# append file input. it will work accordingly with your simple_form wrappers
(out << #builder.file_field(attribute_name, input_html_options)).html_safe
end
end
Then you can do
<%= f.input :avatar, :as => :avatar %>
This is all I needed for this to work (in haml):
=simple_form_for #user, :html => {:multipart => true } do |f|
=f.file_field :image
The code for new/edit views from paperclip's github page looks like this:
<%= form_for :user, #user, :url => user_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
So maybe you should try m.file_field and include :html => { :multipart => true } as well? Though I personally prefer Attachment-Fu.
lets say I have 2 models like News, Clients.
Using paperclip's default options, I need to create for each of them additional columns like (photo_file_name .....)
but I just want to create different model, let's say Asset
asset.rb
belongs_to :client
has_attached_file :photo, :styles => {:small => "300x300>"}
client.rb
has_one :asset, :dependent => :destroy
accepts_nested_attributes_for :asset
clients_controller.rb
def new
#client = Client.new
#client.build_asset
end
_form.html.erb
<%= form_for #client, :html => {:multipart => true} do |f| %>
<%= f.fields_for :asset do |asset| %>
<%= asset.label :photo %><br/>
<%= asset.file_field :photo %>
<% end %>
<% end %>
For now this is working, but how to show it in show view ? i'm doing this:
<%= image_tag #client.url(:small) %>
I know this is not correct, because #client.asset does not have url column,
how to do it ?
Just like Mikhail Nikalyukin said, you should call
<%= image_tag #client.photo.url(:small) %>
instead of
<%= image_tag #client.url(:small) %>
I am trying to create a nested form using formtastic. I've included my code below but am running into some problems that I've also listed below. Any suggestions? Thanks.
# Home model
class Home < ActiveRecord::Base
has_many :home_members
accepts_nested_attributes_for :home_members, :reject_if => :all_blank, :update_only => true, :allow_destroy => true
end
# Form builder in members/new.html.erb
<%= semantic_form_for #home, :url => home_members_path(#home), :html => { :method => :post }, :remote => true do |f| %>
<%= f.inputs do %>
<%= f.semantic_fields_for :home_members do |h| %>
<%= h.input :name %>
<%= h.input :email %>
<%= h.input :birthday, :as => :string %>
<% end %>
<% end %>
# members_controller's new method; #home is set in a before filter
def new
2.times{ #home.home_members.build }
end
A default user is created when a Home is saved. How do I have the form display only the newly created records and not the existing one?
If #1 isn't possible, how do I make the existing record update? I have update_only set on the accepts_nested_attributes_for, but a new record is still created.
I am doing 2.times{ #home.home_members.build } in the controller action. When I print the size of #home.home_members I get 3 (one already exists) as expected. Why is the form only displaying 2 sets of inputs, one being populated with the existing home_member data?
well to answer question 1) show only the newly created objects
# Form builder in members/new.html.erb
<%= semantic_form_for #home, :url => home_members_path(#home), :html => { :method => :post }, :remote => true do |f| %>
<%= f.inputs do %>
<%= f.semantic_fields_for :home_members do |h| %>
<% if h.object.new_record? %>
<%= h.input :name %>
<%= h.input :email %>
<%= h.input :birthday, :as => :string %>
<% end %>
<% end %>
<% end %>
I've used cocoon with success in the past. https://github.com/nathanvda/formtastic-cocoon