# Proper idom for link\_to\_if

**URL:** <https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779>\
**Category:** Rails\
**Created:** [March 24, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779 "2008-03-24T17:48:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779/1 "2008-03-24T17:48:14Z")

</div>

What I wish to do is to have a construct like this in a view:

\<%= link\_to\_if , “New Model View”, new\_model\_path -%\>

has to return true if new\_model\_path is defined and false  
otherwise. What is the proper idiom to do this in rails 2?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779/2 "2008-03-24T17:50:59Z")

</div>

James B. wrote:

defined? new\_model\_path

I do not know why I can spend so much time on something only to find the  
answer after I have asked for help.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779/3 "2008-03-24T17:54:29Z")

</div>

Hi –

On Mon, 24 Mar 2008, James B. wrote:

> What I wish to do is to have a construct like this in a view:
> 
> \<%= link\_to\_if , “New Model View”, new\_model\_path -%\>
> 
> has to return true if new\_model\_path is defined and false  
> otherwise. What is the proper idiom to do this in rails 2?

I guess you could do:

\<% if respond\_to?(:new\_model\_path) %\>  
\<%= link\_to … %\>  
\<% end %\>

but that seems very brittle to me. By the time you’re writing a view,  
you should know what helper methods and named routes exist, and which  
ones don’t.

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
CORE RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for more info!

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779/4 "2008-03-24T18:09:00Z")

</div>

James B. wrote:

But I could not get it to work with link\_to\_if.

This does not work (syntax error)

\<%= link\_to\_if((defined?(new\_vendor\_path)), ‘New Vendor’,  
new\_vendor\_path) -%\>

but this does

\<%= link\_to(‘New Vendor’, new\_vendor\_path) if (defined?  
(new\_vendor\_path)) -%\>

Why?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779/5 "2008-03-24T18:14:25Z")

</div>

David A. Black wrote:

> Hi –

> but that seems very brittle to me. By the time you’re writing a view,  
> you should know what helper methods and named routes exist, and which  
> ones don’t.

Yes and I do/will. But for the moment I am writing one side of a  
bilaterally symmetrical application tree. I therefor wish to make this  
functionality dynamic since I am frequently adding and deleting that  
specific branch. It is not intended for production code.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2008, 6:16pm UTC](https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779/6 "2008-03-24T18:16:55Z")

</div>

On Mon, Mar 24, 2008 at 1:09 PM, James B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> James B. wrote:
> 
> But I could not get it to work with link\_to\_if.
> 
> This does not work (syntax error)
> 
> \<%= link\_to\_if((defined?(new\_vendor\_path)), ‘New Vendor’,  
> new\_vendor\_path) -%\>

I wonder if you would have to write that as:

\<%= link\_to\_if(defined?(:new\_vendor\_path), …

(notice the “:”)?

newbies $.02

–wpd

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2008, 6:42pm UTC](https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779/7 "2008-03-24T18:42:47Z")

</div>

Hi –

On Mon, 24 Mar 2008, James B. wrote:

> Yes and I do/will. But for the moment I am writing one side of a  
> bilaterally symmetrical application tree. I therefor wish to make this  
> functionality dynamic since I am frequently adding and deleting that  
> specific branch. It is not intended for production code.

I also converted your example to a plain if (instead of link\_to\_if) –  
just an artifact of having been playing around with it, but I imagine  
you could translate it back 🙂 But my main point was about knowing  
the helpers, etc., and I understand your point about just trying it  
out.

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
CORE RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for more info!

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779/8 "2008-03-24T18:46:23Z")

</div>

Patrick D. wrote:

> I wonder if you would have to write that as:
> 
> \<%= link\_to\_if(defined?(:new\_vendor\_path), …
> 
> (notice the “:”)?

I tried your suggestion and it does not change anything. I seemingly  
cannot get link\_to\_if to work in this instance.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [March 24, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/proper-idom-for-link-to-if/132779/9 "2008-03-24T21:39:07Z")

</div>

On Mar 24, 2008, at 1:46 PM, James B. wrote:

> Patrick D. wrote:
> 
> > I wonder if you would have to write that as:
> > 
> > \<%= link\_to\_if(defined?(:new\_vendor\_path), …
> > 
> > (notice the “:”)?
> 
> I tried your suggestion and it does not change anything. I seemingly  
> cannot get link\_to\_if to work in this instance.

In order to call the link\_to\_if method, its arguments must first be  
evaluated.  
With David’s change to  
link\_to(…) if defined? new\_vendor\_path  
the arguments to the link\_to() are only evaluated after the “defined?  
new\_vendor\_path” has returned true.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
