I want to display a facebox link when I click on a element when a particular condition is satisfied. I do not want the user to click if the condition is not satisfied. I am doing something like this to achieve it which is pretty ugly
<% if x == 5 %>
<a id="something" href="http://www.some_weird_link.com" rel="facebox">
<% end %>
<div>
<!-- Loads of HTML and erb code here which should be shown no matter what condition -->
</div>
<% if x == 5 %>
</a>
<% end %>
Note that I have to put an if condition just so that the </a>
does not come in my HTML when the condition is not satisfied. This looks bad.
Can you suggest me a better way of doing this ?
link_to_if
is promising but then it wants me to put the link display text in the parameter which happens to be a big <div>
in my case.
I tried a few different cases, and this works (will not link if you pass the option :if => false, will create the link if you pass :if => true, or if you don't pass the if option at all)
Define the helper:
def facebox( *args , &block )
if !args.last.is_a?(Hash) || args.last.fetch(:if,true)
link_to *args , &block
else
block.call
end
end
Once you have that helper, any time you want to use your facebox link method (which you might as well flesh out with all the special facebox arguments -- that rel, for example, could probably be moved into the helper to clean up your view)
Then you can do this in your view:
<% facebox 'http://www.some_weird_link.com' , :id => 'something' , :rel => 'facebox' , :if => (x == 5) do %>
<div>
<!-- Loads of HTML and erb code here which should be shown no matter what condition -->
</div>
<% end %>