adding a method and html options to link_to in rails?

I'm brand new to ruby and rails and I'm having trouble creating a sign-out link (using devise for auth). I want to pass a custom :method parameter into the link_to function, and set a custom class. I seem to be able to do one or the other but not both.

When I try:

<%= link_to "Sign out", destroy_user_session_path, :method => :delete, { :class => 'signout'}   %>

I get the proper result from clicking the link, but I lose my styling. On the other hand, when I try:

<%= link_to "Sign out", destroy_user_session_path, { :class => 'signout'}, :method => :delete %>

I get the styling I want but the link request is passed as GET rather than DELETE, resulting in a routing error.

What am I missing?

Answers


Try placing both :class and :method inside the hash.

So: <%= link_to "Sign out", destroy_user_session_path, { :class => 'signout', :method => :delete} %>


You don't need hash here, because it is already hash :D

link_to "Sign out", destroy_user_session_path, method: "delete", class: "signout"

profit!


Need Your Help

What to choose: activity or viewpager(with fragments)

android

For couple of days, I have a question in my mind that couldn't find any answer for. Right now I have an android app that I use 1 fragment and a view pager with 3 fragments. What I wonder is, is the...