0

In my _updates.html.haml file in my home view of my rails app, I am accessing a bunch of posts with the @posts = Post.tagged_with(params[:tag]) in my home controller. I am doing an each iteration on @posts and am trying to prepend a '#' symbol in front of the tag name so that the '#' is part of the link in the link_to statement.

I am trying the following but I get a typeerror.

  %ul
    - post.tags.each do |tag|
      - tag_plus_hash = '#' + tag
      %li= link_to tag_plus_hash, posts_path(tag: tag.name)

My error is the following:

can't convert ActsAsTaggableOn::Tag into String
Thalatta
  • 4,510
  • 10
  • 48
  • 79

1 Answers1

1

Try calling the method to_s, like so

  %ul
    - post.tags.each do |tag|
      - tag_plus_hash = '#' + tag.to_s
      %li= link_to tag_plus_hash, posts_path(tag: tag.name)

Or even better, use interpolation

  %ul
    - post.tags.each do |tag|
      - tag_plus_hash = "##{tag}"
      %li= link_to tag_plus_hash, posts_path(tag: tag.name)
Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67