Search code examples
ruby-on-railsruby-on-rails-3rel

Rails overwriting rel attribute when supplying method


In Rails 3, when utilizing the method attribute of a link_to, my custom rel value is being overwritten.

# my code:
link_to 'Add to Favorites',
  profile_favorites_path(profile),
  :method => :post,
  :class => 'button',
  :rel => 'favorite'

# expected result:
<a href="/profiles/1/favorites" class="button" data-method="post" rel="favorite nofollow">Add to Favorites</a>

# actual result:
<a href="/profiles/1/favorites" class="button" data-method="post" rel="nofollow">Add to Favorites</a>

Is this a Rails bug / unexpected feature? How can I make use of the built-in method functionality while also supplying a custom rel value?


Solution

  • Seems you can only specify the rel attribute if the method is set to get.

    Inside action_view/helpers/url_helper.rb add_method_to_attributes! you can see the logic rails is currently using for this:

    def add_method_to_attributes!(html_options, method)
      html_options["rel"] = "nofollow" if method && method.to_s.downcase != "get"
      html_options["data-method"] = method if method
    end
    

    This actually makes sense, you wouldn't want to have bots/spiders posting when crawling links.