sscirrus
sscirrus

Reputation: 56719

link_to_unless do

Here's a typical link_to_unless:

<%= link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) %>

How can I do the same thing with a block?

# non-functional
<%= link_to_unless(@current_user.nil?, ..., { :action => "reply" }) do %>
  ...
<% end %>

Upvotes: 1

Views: 2192

Answers (2)

Mohamad
Mohamad

Reputation: 35349

link_to_unless already accepts a block. You need to pass a block argument for to access the link title parameter, though.

link_to_unless(@current_user.nil?, "Name", { :action => "reply" }) do |name|
  link_to name, some_path

At this point though you have to ask yourself if it's just easier to do and if/else statement.

Upvotes: 2

nodrog
nodrog

Reputation: 3532

put this in a helper

def link_to_unless_with_block condition, uri, &block
   link_to_unless condition, capture(&block), uri
end 

should work.. though not tested

Upvotes: 0

Related Questions