0

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?

Cheyne
  • 1,964
  • 4
  • 27
  • 43

3 Answers3

2

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.

0

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' })%>
Wizard of Ogz
  • 12,543
  • 2
  • 41
  • 43
0

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.

yuяi
  • 2,617
  • 1
  • 23
  • 46