Cheyne
Cheyne

Reputation: 2137

Rails link_to_if Class Doesnt Display Correctly

For the life of me, i cant figure out why this problem is happening. I used the link_to helper all the time, but iv only used the link_to_if helper a few times, and this time I cant get the link to take a CSS class.

Here's my link

<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => @test_script), { :class => 'btn btn-mini' })%>

The image displays, with no link as expected, but the CSS class defined at the end does not, instead it just has no class. This is the same format I use in all my link_to helpers.

Can anyone explain why?

Upvotes: 0

Views: 678

Answers (3)

yuяi
yuяi

Reputation: 2725

Wrap the link_to_if or link_to_unless in a span:

 %span.pull-right
   = link_to_unless Foo.deleted.empty?, "<i class='icon-white icon-trash'></i> Undelete Foo".html_safe, deleted_foos_path

Above code sets a css class (pull-right) on whatever is displayed - full link or just its text.

Upvotes: 0

Vivek Pamulapati
Vivek Pamulapati

Reputation: 36

Using link_to_if, only the name --in your case the result of raw('<i class="icon-chevron-up"></i>')-- will be returned if the condition fails. All options that would otherwise apply to the link tag will be ignored.

See the source.

Upvotes: 2

Wizard of Ogz
Wizard of Ogz

Reputation: 12643

Try passing an empty hash as the options argument, so that { :class => '...' } is assigned to the html_options argument. Untested

<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => @test_script), {}, { :class => 'btn btn-mini' })%>

Upvotes: 0

Related Questions