Skip to content

Instantly share code, notes, and snippets.

@adrianrodriguez
Created July 28, 2011 20:14
Show Gist options
  • Save adrianrodriguez/1112438 to your computer and use it in GitHub Desktop.
Save adrianrodriguez/1112438 to your computer and use it in GitHub Desktop.
Adding multiple html attributes in rails
-# I'm trying to add another attribute to this link, but having trouble figuring out how to do it
-# For example, I want to add :style => "display: none" to this line
%strong= link_to "view this", current_user, :class => "public-on"
@jmccartie
Copy link

Even though we just talked about it, adding it here for documentation sake: :)

  • "link_to" is a method that takes three parameters: body, url, and html_options. The first two are strings, the third is a hash (not required)

To see this, add back the full syntax:

link_to("view this", current_user, {:class => "public-on"})

  • So. In order to add another html_option, you simply need to add another "key => value" pair to that hash:

link_to("view this", current_user, {:class => "public-on", :foo => "bar"})

  • If you'd like to strip the extra syntax bits, you'll see how it looks:

link_to "view this", current_user, :class => "public-on", :foo => "bar"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment