Question

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
Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top