undefined method 'to_model' when linking thumbnail to original image - ruby-on-rails

I'm creating an album that I'm planning to display with Masonry and ImgZoom so that the visitors can have a bigger image when they click on it.
According to ImgZoom, to make the zoom work, you need to do the following:
<a href="path/to/real/image.png">
<img src="path/to/image's/thumbnail.png class="thumbnail" />
</a>
So I generated an uploader, with the following inside it:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
'portfolio/photos'
end
version :thumb do
process :resize_to_fit => [220, nil]
end
end
Everything works perfectly, I can call both the versions without trouble, but when I try to follow ImgZoom's instructions by doing the following:
<%= #portfolio.photos.each do |p| %>
#This is a nested form inside the portfolio form, so I need to do this to get my images
<%= link_to image_tag p.image.thumb.url, p.image %>
or:
<%= link_to p.image do %>
<%= image_tag p.image.thumb.url, :class => 'thumbnail' %>
<% end %>
I'm getting the following error: undefined method 'to_model' for #<ImageUploader:0x0000000c35f4d8>
I found a similar subject on stack overflow but the asker wasn't clear and was invited to ask an other question on the forum, which I couldn't find.
I can individually reach 'p.image' and 'p.image.thumb.url', but I can't make a link from one to another, which would be perfectly doable with simple html.
What am I doing wrong?
Thank you in advance

First, to create the class "thumbnail" in the link, you need to declare it properly. I edited the link:
<%= link_to p.image.url do %>
<%= image_tag p.image.url, class: "thumbnail" %>
<% end %>
Second, you need to check if you created an appropriate route for viewing the image. This can be either done by linking to a static assets properly (as your image is not under "public") or via a template view.
If the files where stored under "public", your way of linking should work just fine.
Check out how image_path works in the docs: image_path (and more)

Related

Is there a way to loop through many attached images using active storage

I have installed active storage and I want a Post class or model to have one or many images so I created an association like below:
class Post < ApplicationRecord
has_many_attached :images
end
I can create a post and I have allowed the images in the parameters and I have enabled multiple uploads and the post can be created successfully but when I want to iterate the images on the view like below I get nothing;
<% #post.images.each do |image| %>
<%= image_tag(image) %>
<%end%>
When I try to just display one image without looping like below I get an error saying Can't resolve image into URL: undefined method to_model'`.
<%= image_tag(#post.images) %>
So far that is what I have tried and when I just display the #post.images in an embedded ruby I can get some information like below that indeed the images are there.
Is there anything am doing wrong?
I implemented this previously by doing it this way.
class Post < ApplicationRecord
has_many_attached :images
end
The images for your post should included in the params as such:
def car_params
params.require(:car).permit(:name, :description, :comments, images:[])
end
Then in the views, I looped through the images this way:
<% if #post.images.attached? %>
<% #car.images.each do |image| %>
<li>
<button type="button" >
<%= image_tag(url_for(image)) %>
</button>
</li>
<% end %>
<% end %>
If you want to select one image without looping, then you can select by the index.
<%= image_tag(#post.images[0]) %>
If you seek to loop through, go ahead with the url tag as that works better.

How do I add an image placeholder to fix this bug

I am a beginner programmer. I'm building my first project which shows a list of my favorite athletes and when clicked the "show" page loads an image of them, their name, and weight.
Overall when adding a new athlete everything works fine. If I decide to add a new athlete and skip adding an image I get this error "Can't resolve image into URL: undefined method `persisted?' for nil:NilClass", it also highlights my code "<%= image_tag(#fighter.image) %>" that's in my show.html.erb
I've tried adding an alt: but I get another error of "wrong number of arguments (given 1, expected 0)".
I've searched for answers and one thread mentioned my controller page and the params section. I did notice that (:image) is preceded by "params.require" which I would assume could be the problem if I'm not adding an element that is required. How do I solve this issue? What would be the best way to add a placeholder, I've added a default.png in my images folder.
Thank you
show.html.erb
fighters_controller.rb
If you're using ActiveStorage for uploading athlete images. You can check if an image was attached to your model with #fighter.image.attached?.
So I would suggest wrapping the image_tag in an IF-clause like so:
<% if #fighter.image.attached? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
If you're using Paperclip then try the following:
<% if #fighter.image.exists? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
With Carrierwave use:
<% if #fighter.image.present? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>

Can not move to page when I click the image (using gem 'rinku')

I'm using Gem:Rinku and trying to combine an image with a link that someone add from browser.
But there is no image on the browser when I write below code and just url link on the page.
show.html.erb
<div class="posted_share_item_link">
<%= Rinku.auto_link(#item.item_link, :all, 'target="_blank"').html_safe do %>
<%= image_tag "amazon_logo.jpg", class:"amazon_image" %>
<% end %>
</div>
I thought it works if I use do-end and put image_tag in it but actually it doesn't.
so, that would be great if someone knows how I can fix it.
Thank you.
the link is not active
Here's the fixed version.
<div class="posted_share_item_link"><%= Rinku.auto_link(#item.item_link, :all, 'target="_blank"') do %>
<%= image_tag "amazon_logo.jpg", class:"amazon_image" %>
<% end %>.html_safe
You should call html_safe on end of chain instead of middle. In your version, you passed block to result of html_safe which is string and that's why it didn't work

Rails 5: image and loop of images are not showing in email

I have this in the mailer:
attachments.inline["logo.png"] = File.read("#{Rails.root}/app/assets/images/logo.png")
And in the email template I have this:
<%= image_tag attachments['logo.png'].url %>
It works just fine and the logo does appear in the email.
Now this is the weird part, I have another image that is not in the asset pipeline but stored in the database and also a loop of other images stored in the database. And none of them appear in the email. The email goes through without any errors. Any idea what might be wrong and how I can debug this?
<%= image_tag #account.image.thumb.url %>
<% #attachments.each do |attachment| %>
<%= image_tag attachment.images.thumb.url %>
<% end %>
I'm using the gem carrierwave to attach image and I uploading this version:
version :thumb do
process resize_to_fill: [1024, 768]
end
There are two options here: either your images, stored in the databases, also have a public-facing url, and then you can use those. But most likely they do not, and then you have to add the images as attachments to your email as well, and then use the attachments when building the email.
So in your mailer write something like:
#attachments.each do |attachment|
attachments.inline[attachment.image.original_filename] = attachment.image.thumb.read
end
and then in your mail view, you can just iterate over all #attachments again, and use the correct attachments.inline[...], something like
<% #attachments.each do |attachment| %>
<%= image_tag attachments.inline[attachment.image.original_filename].url %>
<% end %>
Note: I am not entirely sure of the correct syntax here (you use images but I presume it should be singular? also you should check what is the best unique way for your image, maybe the original_filename is not ideal, you could also just use the id of the attachment.

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.

Resources