Rails Nested File Upload: Edit Content - ruby-on-rails

I built an ajax upload form for image uploads with RoR which works well so far. The only problem I am facing is when editing content that has images attached to it. I want the images to show up as thumbnails (no problem) and below that a new upload field.
This is how I show the upload field normally in the view...
<%= form.fields_for :images do |builder|%>
<p><%= builder.label :image %></p>
<p id="ajax_upload"><%= builder.file_field :image %></p>
<% end %>
If I do this for editing content I get as many upload fields as images.. I just want one...

I generally do not refer to symbols for form builders, as they tend to behave in unexpected ways sometimes. You should be able to send in an object instance like so:
<% #image = #the_model.images.first %>
<%= form.fields_for #image do |builder|%>
<p><%= builder.label :image %></p>
<p id="ajax_upload"><%= builder.file_field :image %></p>
<% end %>

Related

Ruby on Rails | I want to fetch Images of Best Seller Taxonomy from ActiveStorage Title is Fetched and display on Screen But Images are not

<%best_sellers = Spree::Taxonomy.find_by_name("Best Seller").root.products.limit(6)%>
<%best_sellers.each do |best_seller_array|
<div class="card card-custom text-center rounded w-25">
<%= image_tag main_app.url_for(best_seller_array&.attachment),class:"img-fluid" %>
<%= best_seller_array.name%>
</div>
<%end%>
In spree, product has_many :images, so if you want to display any product image, you can try something like below -
<%= image_tag main_app.url_for(product&.images&.first&.attachment), class: "img-fluid" %>
and if you want to display all the images of the product, then simply traverse the loop of images like below
<% best_sellers_products.each do |product| %>
<% product.images.each do |image| %>
// display image
<% end %>
<% end %>
Hope this will solve the problem.
Thank you

Fedena Image in PDF Report using wicked_pdf

I am trying to insert image in student report, edited the below code and it shows the default image but when I upload the student image it doesn't show it.
<% if #student.photo.file? %>
<%= wicked_pdf_image_tag #student.photo.url %>
<% else %>
<%= wicked_pdf_image_tag "master_student/profile/default_student.png" %>
<% end %>

Paperclip displaying multiple 'missing image' tags on edit page

So I have an object that has multiple photos (through use of a model called asset). I'm using paperclip to upload the photos and it's working fine. On my show page I call
<li class="grid_3">Photos:<br /></li>
<% #address.assets.each do |asset| %>
<li class='grid_3'><%= image_tag(asset.photo.url, size: '100x100') %></li>
<% end %>
and the photos display perfectly. when I click on the edit page:
<%= f.simple_fields_for :assets do |asset| %>
<%= render :partial => 'asset_fields', :locals => { :f => asset } %>
<% end %>
the partial is rendered:
<div class='nested-fields'>
<%= f.file_field :photo %>
<% unless f.object.new_record? %>
<%= f.file_field :photo %>
<% #address.assets.each do |asset| %>
<li class='grid_3'><%= image_tag(asset.photo.url, size: '100x100') %></li>
<% end %>
<% end %>
</div>
at this point the photos each show up 3 times along with an extra image tag with a 'missing' photo placeholder. I would say I've tried different things but I just don't see what is wrong with the code. Any ideas?
Assuming simple_fields_for behaves the same as fields_for... the call to f.simple_fields_for :assets is a loop already. It loops over f.object.assets and renders the contents of the block once for each item found using the asset form generator (it may be a little less confusing to call that block variable asset_form, by the way). So since you're then rendering a partial that also renders all of the assets for the #address that's why you get each image 3 times.

Trouble rendering images uploaded with CarrierWave

I'm building a basic Rails 4 application, and seem to have hit a frustrating snag. I've been following the CarrierWave Railscast, and while I'm able to get images to show up on /image/show.html.erb, I've had some difficulties with getting any of the images I've uploaded to display in the gallery each image is associated with.
The strange thing is, there aren't any errors logged. The page loads without any Rails errors coming up in the page or the terminal; the only way I know something is wrong is that the div that images are supposed to appear in doesn't show up at all.
I'm really stumped about this. If you look, the .images div in the show action for galleries renders, but absolutely none of the sub-elements render at all. What am I doing wrong?
Source code here
app/models/image.rb
class Image < ActiveRecord::Base
belongs_to :gallery
mount_uploader :image, ImageUploader
end
app/models/gallery.rb
class Gallery < ActiveRecord::Base
has_many :images
end
app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_limit => [200, 200]
end
end
apps/views/images/show.html.erb
Below, we can see that images can be rendered without issue, as this is a view for their own respective controller.
<p>
<strong>Title:</strong>
<%= #image.title %>
</p>
<p>
<strong>Description:</strong>
<%= #image.description %>
</p>
<p>
<strong>Image:</strong>
<%= image_tag #image.image_url.to_s %>
</p>
/apps/views/galleries/show.html.erb
This is where everything gets tricky. First off, no matter what I change within the images div, everything within it seems to come up blank regardless. I've tried changing the "for image in #gallery.images" bit several times, but to no avail.
<div id="images">
<% for image in #gallery.images %>
<div class="image">
<%= image_tag image.image_url(:thumb) %>
<%= image.description %>
<div class="name"><%= image.title %></div>
<div class="actions">
<%= link_to "edit", edit_image_path(image) %> |
<%= link_to "remove", image, :confirm => 'Are you sure?', :method => :delete %>
</div>
</div>
<% end %>
<div class="clear"></div>
</div>
<p>
<%= link_to "Add a Painting", new_image_path(:gallery_id => #gallery) %> |
<%= link_to "Remove Gallery", #gallery, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View Galleries", galleries_path %>
</p>
You need to associate the gallery and the image. One option is to pass the gallery_id to the new action like you do now, set it in the controller on the new image, add a hidden field to transport it to the create action and whitelist the parameter there.
The other option is to nest the image routes into the gallery routes like so:
resources :galleries do
resources :images
end
This will create URLs like /galleries/123/images/234 and galleries/1233/images/new. You create links to these page for example with edit_galleries_image_path(#gallery, #image). Again you would need to set the correct gallery_id in images#new on the new image but that should then let form_for generate the right path to the create action where you then have the gallery_id parameter available. Going this way rake routes should come in as a handy tool.

How do I get multiple paperclip uploads to display in the view?

I'm very new to programming and have followed this screencast at emersonlackey.com on adding multiple paperclip uploads for a record. It all works great but I can't figure out how to display the uploaded images in the records show page.
They display fine on the edit page using:
<%= f.fields_for :venuephotos do |photo| %>
<% unless photo.object.new_record? %>
<p>
<%= link_to image_tag(photo.object.venuephoto.url(:thumb)), photo.object.venuephoto.url(:original) %>
<%= photo.check_box :_destroy %>
</p>
<% end %>
<% end %>
I've used paperclip in other models but they are the standard each record only has one upload so the images display fine with <%= #model.photo.url %> type call but I cant figure out this other way.
Any help is much apprectiated!
You want to do something like this:
<% for asset in #post.assets %>
<%= link_to image_tag(asset.asset.url(:thumb)), asset.asset.url(:original) %>
<% end %>
as per the code that goes along with the screencast.
https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3/blob/master/app/views/posts/show.html.erb

Resources