views:

58

answers:

3

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.

+1  A: 

Try

<%= "<a id='something' href='http://www.some_weird_link.com' rel='facebox'>" if x == 5 %>
<div>
    <!-- Loads of HTML and erb code here which should be shown no matter what condition -->
</div>


<%= " </a> " if x == 5 %>
Salil
Thanks... Its better than what I have. But i want to get rid of the double ifs..
Shikher
+3  A: 

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 %>
Joshua Cheek
Thank you so much Joshua.... This is really good.. View is so clean.
Shikher
this is a really cool code snippet. thanks Joshua
sameera207
A: 

hi Shikher

If I understood your question correctly this should do the trik

<%= ((x == 5) ? (link_to 'facebook', 'http://www.some_weird_link.com', :id => 'abc', :rel => 'facebox') : '') %>

cheers

sameera

sameera207
Hey Sameera... Thanks for the reply. The problem is that the link_to text ('facebook' in your answer) is actually a big <div> .... </div> which had html+erb content.So I could not use something like this.Appreciate you help though :) Thanks!
Shikher
Hi,Then try something like this, create a helper method def my_link <create your html+erb and NOTE : create your link also inside this>endEx: def my_link "<p>This is a huge text and it has a long string but it works as a link also if you click" + <a href='url'>here</a> + "</p>"endand call it like this<%= ((x == 5) ? (my_link : '') %>The idea here is add your complexity to the helpercheerssameera
sameera207