Disable link on two conditions


#1

I want to disable my navigation depending on two conditions: 1) the
page is not the current one, 2) the page is not in the session.

I have tried:
<%= link_to_unless @page.nil? or current_page?, “My ICA
Pages” , :controller => ‘page’, :action => ‘list’%>
–gives me errors
<%= link_to_unless( @page.nil?,"",{})do link_to_unless_current “My ICA
Pages” , :controller => ‘page’, :action => ‘list’%>
–no link is ever displayed

any suggestions. Code examples are always helpful.

Thanks in advance, K


#2

any suggestions. Code examples are always helpful.

Haven’t tried it, but maybe…

link_to_unless (@page.nil? || current_page?(params)), “blah blah
blah”…

-philip


#3

Consider using @page.blank? instead of @page.nil?. Also, consider
inserting
this above the statement:

<% RAILS_DEFAULT_LOGGER.debug("page was: #{@page} and current_page? is
#{current_page?}) %>
<% RAILS_DEFAULT_LOGGER.debug("My or expression evaluates to:
#{@page.nil?
or current_page?}) %>
<% RAILS_DEFAULT_LOGGER.debug("My || expression evaluates to:
#{@page.nil?
|| current_page?}) %>

If you tail your development.log, it should give you an idea what’s
going
on.

Kim G. wrote:

Pages" , :controller => ‘page’, :action => ‘list’%>
–no link is ever displayed

any suggestions. Code examples are always helpful.

Thanks in advance, K


View this message in context:
http://www.nabble.com/disable-link-on-two-conditions-tf3397451.html#a9460550
Sent from the RubyOnRails Users mailing list archive at Nabble.com.


#4

Sorry, should have explained myself better.

@page is a session variable so @page.nil? is correct. I want to check
if the session contains the page (which is a model in my app)

I am well aware on how to debug, thanks anyways.

The problem is that the things I have tried are not formatted
correctly, thats what I am looking for.

How do I use two conditions in the link_to_unless or if I can’t, how
can I use the block statement in link_to_unless to then call
link_to_unless_current, or any other suggestions on how to disable a
link depending on two conditions.

Thanks,
Kim


#5

On Mar 13, 2007, at 2:43 PM, Kim wrote:

<%= link_to_unless( @page.nil?,"",{})do link_to_unless_current

If you tail your development.log, it should give you an idea
The problem is that the things I have tried are not formatted
correctly, thats what I am looking for.

How do I use two conditions in the link_to_unless or if I can’t, how
can I use the block statement in link_to_unless to then call
link_to_unless_current, or any other suggestions on how to disable a
link depending on two conditions.

Thanks,
Kim

Your problem is initially using ‘or’ which is very low precedence:
<%= (link_to_unless @page.nil?) or (current_page?, “My ICA
Pages” , :controller => ‘page’, :action => ‘list’) %>

–gives me errors
With the precedence made explicit, the reason for errors should
become obvious.

<%= link_to_unless( @page.nil?,"",{}) do link_to_unless_current “My
ICA Pages” , :controller => ‘page’, :action => ‘list’%>

–no link is ever displayed

Do you have a test? I suspect that you don’t, but I’ll leave that as
an exercise.

<%= link_to_unless(@page.nil? || current_page?(:controller =>
‘page’, :action => ‘list’),
“My ICA Pages”, :controller => ‘page’, :action =>
‘list’) %>

Which will be a link having anchor text “My ICA Pages” or just the
plain text if either condition is true. If you want different text
for the non-link case:

<%= link_to_unless(@page.nil? || current_page?(:controller =>
‘page’, :action => ‘list’),
“My ICA Pages”, :controller => ‘page’, :action =>
‘list’) {|txt| “No link to #{txt}”} %>

If you want nothing at all, then use:

<% unless @page.nil? || current_page?(:controller => ‘page’, :action
=> ‘list’) %>
<%= link_to(“My ICA Pages”, :controller => ‘page’, :action =>
‘list’) %>
<% end %>

Does that get you where you’re trying to go?

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#6

Thanks, yes it does get me were I was trying to go.

On 3/13/07, Rob B. removed_email_address@domain.invalid wrote:

<%= link_to_unless @page.nil? or current_page?, "My ICA
Consider using @page.blank? instead of @page.nil?. Also, consider
#removed_email_address@domain.invalid?

Thanks,
<%= link_to_unless( @page.nil?,"",{}) do link_to_unless_current "My

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid


Kim G.
removed_email_address@domain.invalid

“We are all stakeholders in the Karma economy.”