mbajur
mbajur

Reputation: 4474

Adding a class to link_to block

I have a following code which displays a 'delete' link:

<%= link_to :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' }  do
  <span>Delete</span>
<% end %>

But for some reason ROR is not adding some_class to a tag. Have you any idea what can i do to fix it ? Thanks in advance.

Upvotes: 8

Views: 8368

Answers (4)

Fernando Kosh
Fernando Kosh

Reputation: 3593

If you need to pass a controller and action, like edit and destroy, do it as follow:

<%= link_to url_for(controller: controller_name, action: :edit, id: item.id), class: "btn btn-link btn-warning btn-just-icon edit" do %>
  <i class="material-icons">edit</i>
<% end %>
<%= link_to url_for(controller: controller_name, action: :destroy, id: item.id), method: :delete, data: { confirm: t('common.confirm') }, class: 'btn btn-link btn-danger btn-just-icon remove' do %>
  <i class="material-icons">close</i>
<% end %>

Upvotes: 0

dillonbailey
dillonbailey

Reputation: 31

I actually found this to be a working solution with Rails 4.2

<%= link_to(resource_path(@resource), class: "project-card clearfix") do %>
    <h1>Your html here</h1>
<% end %>

Upvotes: 3

Teoulas
Teoulas

Reputation: 2963

You need to add the URL as the first parameter, then the html options, e.g.:

<%= link_to resource_path(@resource), :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' }  do
  <span>Delete</span>
<% end %>

Upvotes: 25

RadBrad
RadBrad

Reputation: 7304

The link_to docs:

link_to(body, url, html_options = {})

So you'd want

<%= link_to <span>Delete</span>, '/someurl', :class=>'some_class', :method=>:delete, .... %>

Upvotes: -1

Related Questions