How to add multiple images using carrierwave - ruby-on-rails

i created form, but on click ,choose image option is not appending to add multiple images.
Please give me a solution for this
my property form.html.erb
<%= simple_nested_form_for ([:generic_users, #property]) do |f| %>
<%= f.input :image, required: true %>
<p><%= image_tag #property.image_url(:thumbnail) if #property.image? %></p>
<%= f.hidden_field :image_cache %>
<%= f.simple_fields_for :property_images do |i| %>
<%= i.input :image %>
<p><%= image_tag i.object.image_url(:thumbnail) if i.object.image? %></p>
<%= i.hidden_field :image_cache %>
<%= i.link_to_remove 'Remove',method: :delete, class: 'btn btn-danger'%>
<% end %>
</div>
</div>
<hr>
<%= f.link_to_add 'Add Image', :property_images, class: 'btn btn-primary', data: {target: '.additional-images'} %>
<hr>
<% end %>
this is my propertyImage model
class PropertyImage < ActiveRecord::Base
belongs_to :property
validates_presence_of :image
mount_uploader :image, ImageUploader
end
my property.rb
class Property < ActiveRecord::Base
has_many :property_images, dependent: :destroy
accepts_nested_attributes_for :property_images, allow_destroy: true
end
This is how i am getting page , on clicking Add Image , multiple option for choose image is not appearing.
Please give me a solution

It's not clear to me from your code where the actual file input is being rendered. However for multiple file selections you would want code something like this:
<%= f.file_field :image, multiple: true, require: true %>

You can use
<%= file_field_tag "images[]", type: :file, multiple: true %>
instead of <%= f.file_field :image, multiple: true, require: true %>
if you will upload multiple images then there will be an array of images will be create in the from parameter with name images[]

Related

Showing if there is an image in a nested form with Carrier Wave gem

I'm doing a big nested form, where the user can upload an image for each nested form.
But I have a doubt. How can I show to the user, when he edit's the form, if there is already an image uploaded to each nested form? I thought of showing the image file name next to the file loader input, but i have no idea how to do that.
Here's my code:
Model
class Chapter < ActiveRecord::Base
attr_accessible :content, :reference, :story_id, :image, :decisions_attributes
mount_uploader :image, ImageUploader
end
View
<%= simple_form_for(#story) do |f| %>
<%= f.simple_fields_for :chapters do |builder| %>
<%= render "chapters_fields", f: builder %>
<% end %>
<p>
<%= link_to_add_fields "Add chapters", f, :chapters, "chapters" %>
</p>
<% end %>
Form Partial
<div class="chapter-fields">
<h3>Chapter</h3>
<p>
<%= f.input :reference %>
<%= f.input :content, as: :text, input_html: { rows: 10, style: 'width: 100%' } %>
<%= f.input :image %>
<%= f.image_url %>
</p>
<div>
<%= f.simple_fields_for :decisions do |builder| %>
<%= render "decisions_fields", f: builder %>
<% end %>
<%= link_to_add_fields "Add decisions", f, :decisions, "decisions" %>
</div>
<p><%= link_to_remove_fields "Remove chapter", f, "chapter" %></p>
</div>
You can access the object behind a form builder variable via the object keyword.
if f.object.image.blank?
"No current image"
else
"Current image: #{f.object.image.url}"
end
Note: This only works if you have accepts_nested_attributes_for :chapters in your Story model.

How delete paperclip item with checkbox

I have the next code in a partial _form of my view:
<%= f.label :logo %><br />
<% if f.object.new_record? %>
<%= f.file_field :logo %>
<% elsif %>
<%= link_to image_tag(f.object.logo.url(:thumb)), f.object.logo.url(:original) %>
<%= f.check_box %>
<% end %>
Im try to delete the object.logo if i select the checkbox and press the edit button. Im do it with Nested items, but this is a simple paperclip implementations.
any help is welcome, thank you.
You can do domething like this:
In the model:
accepts_nested_attributes_for :logos, :allow_destroy => true
In the form
if !f.object.logo_file_name.blank?
f.input :_destroy, :as => :boolean, :label => "Delete?"
end
Hope this help
Today i get the solution to this issue:
In the brand model:
attr_accessible :description, :title, :logo, :delete_logo
#delete existing logo from edit view with checkbox.
attr_accessor :delete_logo
before_validation { logo.clear if delete_logo == '1' }
In the brand view _form:
<% if #brand.logo? %>
<%= link_to image_tag(f.object.logo.url(:thumb)), f.object.logo.url(:original) %>
<%= f.check_box(:delete_logo) %>
<% else %>
<%= f.file_field :logo %>
<% end %>
Works for me.

rails, carrierwave, multiple images

I am working on my first rails application and I am attempting to setup multiple image uploads in a form. I have created a contest which has many contest entries, and contest entries can have many images. I have the contest entry piece working, but I am having trouble getting the image upload portion of the form working. I am following this tutorial http://lucapette.com/rails/multiple-files-upload-with-carrierwave-and-nested_form/ linked to from the carrier wave wiki.
contest entry model
class ContestEntry < ActiveRecord::Base
attr_accessible :body, :title, :contest_id, :entry_images_attributes
validates :user_id, presence: true
validates :title, presence: true, length: { maximum: 140 }
validates :body, presence: true
validates :contest_id, presence: true
belongs_to :contest
belongs_to :user
has_many :entry_images, as: :imageable
accepts_nested_attributes_for :entry_images
end
Entry image model
class EntryImage < ActiveRecord::Base
attr_accessible :alt, :image_path
belongs_to :imageable, :polymorphic => true
validates :image_path, presence: true
mount_uploader :image, ImageUploader
end
New entry form
<%= nested_form_for([:contest, #entry], :html => {:multipart => true}) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.hidden_field :contest_id %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<%= f.fields_for :entry_images do |builder| %>
<p>
<%= builder.label :alt %><br />
<%= builder.text_field :alt %>
</p>
<p>
<%= builder.label :image_path %><br />
<%= builder.file_field :image_path %>
</p>
<% end %>
<p><%= f.link_to_add "Add Image", :entry_images %></p>
<%= f.submit "Enter", class: "btn btn-large btn-primary" %>
<% end %>
<%= javascript_include_tag :defaults,"nested_form" %>
for some reason nothing shows up in the <%= f.fields_for :entry_images do |builder| %> block. It is just blank there is also no error message. if I switch it to
<%= f.fields_for :entry_image do |builder| %> and switch to using a form_for instead of the nested_form_for plugin all the fields show up but I get errors when I submit.
Any ideas?
Thanks,
Cory

nested form is not displayed

I have the following form declaration for a new kindergarten
<%= form_for #kindergarten, :html => {:multipart => true} do |f|%>
<%= render 'shared/error_messages', object: f.object %>
</br>
<%= f.fields_for :photos do |p| %>
<%= p.label 'upload photo'%>
<%= p.file_field :image %>
<% end %>
</br>
<%= render 'about_company', f: f%>
</br>
<%= render 'contact', f: f %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<%end%>
The logic behind this is that 1 kindergarten can have multiple photos.
Here are the model declarations:
Kindergarten
has_many :photos, limit: 7, dependent: :destroy
accepts_nested_attributes_for :photos
Photo
attr_accessible :image, :logo, :kindergarten_id
belongs_to :kindergarten
mount_uploader :image, ImageUploader
validates :kindergarten_id, presence: true
validates :photo, presence: true
I am struggling with this for a few hours now, and I can't understand why the "upload file" button and label are not showing up. When i say now showing up, i mean they are completely ignored and not present in the rendered html.
Before rendering the form for a new # kindergarden, you'll need to build a photo association for this to work:
def new
#kindergarden = Kindergarden.new
#kindergarden.photos.build # provides a photo object to use in the form
# rest of your action
end
Also, if you wanted multiple photo fields in the form, you could do something like:
5.times { #kindergarden.photos.build }

Can't upload photo using paper clip

I used paper clip in my web application, I use this to make a new product:
<% semantic_form_for #product do |f| %>
<% f.inputs do %>
<%= f.input :title %>
<%= f.input :price %>
<%= f.file_field :photo %>
<%= f.input :category , :include_blank => false %>
<% end %>
<%= f.buttons %>
<% end %>
And this to show product:
<% semantic_form_for #product do |f| %>
<%= image_tag #product.photo.url%>
<% f.inputs do %>
<%= f.input :title %>
<%= f.input :price %>
<%= f.file_field :photo %>
<%= f.input :category , :include_blank => false %>
<% end %>
<%= f.buttons %>
<% end %>
And his is my product.rb:
class Product < ActiveRecord::Base
validates_presence_of :title, :price
validates_numericality_of :price
validates_uniqueness_of :title
has_attached_file :photo
attr_accessible :name, :category_id, :price, :title, :photo
belongs_to :category
has_many :order_items
end
But after I upload the photo, it show my image path like this :
http://localhost:3000/photos/original/missing.png
It seems that it can't upload the photo with this error:
No route matches "/photos/original/missing.png" with
{:method=>:get}
I am not sure what semantic_form_for is, but with regular form_for, you have to explicitly say it's a multipart form.
<% form_for(#thing, :html => {:multipart => true}) do |f| %>
....
If you don't do this, the contents of your file upload fields won't be transmitted to the server. It's a HTML thing ;)
I think you paste the image into public/images folder in your rails application.And give the path like in your browser "/images/missing.png".
And your index will be

Resources