0

I have Questions with Tags.

When you click a tag name, it takes you to /tagname.

E.g. clicking ruby takes you to /ruby.

But, there are some tag names that need to be HTML escaped (e.g. c# or .net, etc.).

How do I do that?

I tried doing params[:tag].html_safe in my controller, but that doesn't do it because the params are being sent incorrectly before.

E.g. this is what the log looks like when I do click on the c# tag (or I manually type in c# in the URL).

Started GET "/c" for 127.0.0.1 at 2013-03-29 07:31:46 -0500
Processing by HomeController#index as HTML
  Parameters: {"tag"=>"c"}
  Tag Load (26.7ms)  SELECT "tags".* FROM "tags" WHERE "tags"."name" = 'c' LIMIT 1

This is what my route looks like:

get ':tag', to: 'home#index', as: :tag
marcamillion
  • 32,933
  • 55
  • 189
  • 380

1 Answers1

1

Add a 'slug' column to your Tag model, and store a name in a normalized form in it:

class Tag < ActiveRecord::Base 
  before_create :build_slug

  def build_slug
    self.slug = name.parameterize
  end
end

Add unique index on 'slug' column and find tags with Tag#find_by_slug

Or take a look at 'friendly_id' gem: https://github.com/norman/friendly_id

cthulhu
  • 3,749
  • 1
  • 21
  • 25
  • That doesn't solve my issue. The tag slugs are still not the same as the name, in those cases (i.e. `c#`, `c++`, `.net`, etc.) – marcamillion Mar 29 '13 at 17:00
  • You can see more details at this question - http://stackoverflow.com/questions/15707692/friendlyid-doesnt-build-html-escaped-slugs – marcamillion Mar 29 '13 at 17:06
  • Well, there is no point in having slug same as name, the point is to have slug in form that is allowed in URL – cthulhu Mar 30 '13 at 08:45