Jake
Jake

Reputation: 1380

Convert an image_tag to a view helper in Rails

So I'm using the following in both my show and index:

 <%= image_tag posts.avatar(:large), alt: 'Avatar for #{posts.title}', class: 'img-responsive' %>

Since they are the same in both I'm trying to move them to a view helper. Then in my views I'm putting:

<%= post_image_tag %>

My initial take was the following:

def post_image_tag
 image_tag posts.avatar(:large), alt: 'Avatar for #{posts.title}', class: 'img-responsive'
end

I end up with : undefined local variable or method `posts' for #<#:0x007fcdd273e860> Did you mean? @posts

Cool. So I change it to:

def post_image_tag
 image_tag @posts.avatar(:large), alt: 'Avatar for #{posts.title}', class: 'img-responsive'
end

Now I end up with: undefined method `avatar'.

So I decided that it might just be that I'm not referencing it correctly and trying to pull ActiveRecord on a single post so I try:

def post_image_tag
  @posts.each do |posts|
  image_tag posts.avatar(:large), alt: 'Avatar for #{posts.title}', class: 'img-responsive'
 end
end

At this point something FINALLY renders on the page. Except it looks like an HTML nightmare with:

[#<SpudPost id: 1, spud_user_id: 2, title: "The Top ", content: "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transi...", comments_enabled: false, visible: true, published_at: "2018-01-30 14:45:58", created_at: "2018-02-05 17:41:42", updated_at: "2018-02-05 17:41:42", url_name: "the-top", is_news: false, meta_keywords: nil, meta_description: nil, content_format: "HTML", content_processed: nil, blog_key: "blog", custom_author: nil, identifier: "9b0d97c9-6855-4ad6-85ac-cade6012b5de", avatar_file_name: "ice.jpg", avatar_content_type: "image/jpeg", avatar_file_size: 68494, avatar_updated_at: "2018-02-05 17:41:40">, 

It goes further repeating the next item and the next, etc. What in the world can I put on the image_tag to make it render correctly? I've also tried changing the view helper in my view to:

<%= raw(post_image_tag)%>

Then I end up with [#, #, #, #]

Upvotes: 0

Views: 126

Answers (2)

Pablo
Pablo

Reputation: 3005

The image is for 1 post, so you should name the variable 'post' and not 'posts'. Anyway this is just for clarity and not an error.

You can define a helper that accepts a param:

def post_image_tag post
 image_tag post.avatar(:large), alt: 'Avatar for #{post.title}', class: 'img-responsive'
end

and call it this way (assuming post is 1 post):

<%= post_image_tag(post) %>

If you have many posts in @posts, you should do:

<%= @posts.each do |post| %>
  <%= post_image_tag(post) %>
<% end %>

You can also make another helper to handle many posts (it may need some changes, but you get the idea):

def posts_image_tag posts
  result=''
  posts.each do |post|
    result += post_image_tag(post)
  end
  result.thml_safe
end

and call it this way (assuming @posts has all your posts):

<%= posts_image_tag(@posts) %>

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230531

You took a wrong turn in this rabbit hole. Go back to your first version of the helper and pass the post as parameter

def post_image_tag(post)
 image_tag(post.avatar(:large), alt: 'Avatar for #{post.title}', class: 'img-responsive')
end


# this "posts" should really be named "post", since it's a single post,
# not a collection of them.
<%= post_image_tag(posts) %>

Upvotes: 2

Related Questions