# Render partial -- show certain content only if

**URL:** <https://www.ruby-forum.com/t/render-partial-show-certain-content-only-if/101378>\
**Category:** Rails\
**Created:** [February 5, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/render-partial-show-certain-content-only-if/101378 "2006-02-05T21:49:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 5, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/render-partial-show-certain-content-only-if/101378/1 "2006-02-05T21:49:36Z")

</div>

i’m working on a \_post.rhtml partial, and it displays one blog post.  
i’m accessing this partial on the list.rhtml and the show.rhtml

what i’d like is to only show the “link” link on the list.rhtml and not  
on the show page.

```
<%= post.title%>
        <br />
    <%= post.body%>

    <%= link_to 'link', :action => 'show', :id => post %> |

    <%= link_to 'Edit', :action => 'edit', :id => post %> |
    <%= link_to 'Destroy', { :action => 'destroy', :id => post },

```

:confirm =\> ‘Are you sure?’ %\>

thanks in advance for the help

---

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 5, 2006, 10:00pm UTC](https://www.ruby-forum.com/t/render-partial-show-certain-content-only-if/101378/2 "2006-02-05T22:00:15Z")

</div>

matthew collins wrote:

> i’m working on a \_post.rhtml partial, and it displays one blog post.  
> i’m accessing this partial on the list.rhtml and the show.rhtml
> 
> what i’d like is to only show the “link” link on the list.rhtml and not  
> on the show page.

I can think of two ways. When you render your partial you can pass in  
parameters as well:

\<%= render :partial =\> “post”, :locals =\> { :allow\_link =\> true } %\>

or

\<%= render :partial =\> “post”, :locals =\> { :allow\_link =\> false } %\>

Then your partial code can become smarter:

\<%= link\_to\_if allow\_link, ‘link’, :action =\> ‘show’, :id =\> post %\> |

Alternatively, don’t put the “link\_to ‘link’…” in the partial, put it  
only in the list.rhtml:

# list.rhtml

\<%= render :partial =\> “post” %\>  
\<%= link\_to ‘link’, :action =\> ‘show’, :id =\> post %\>

The drawback here is I’m not sure it would flow well if you’re trying to  
get all the links on the same line.

Jeff

> **[Hey Daddio!](http://www.softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 5, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/render-partial-show-certain-content-only-if/101378/3 "2006-02-05T22:53:00Z")

</div>

Jeff C. wrote:

> The drawback here is I’m not sure it would flow well if you’re trying to  
> get all the links on the same line.
> 
> Jeff  
> [www.softiesonrails.com](http://www.softiesonrails.com)

thanks for the advice.

i had to go with having the link in the list.rhtml page only. the other  
method, it would still show the word “link”, it just wouldn’t be linked.

---

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 5, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/render-partial-show-certain-content-only-if/101378/4 "2006-02-05T22:56:01Z")

</div>

You can also do something like this:

\<% if params[:action] == “list” %\>  
\<%= render :partial =\> “poloroids” %\>  
\<%= render :partial =\> “demo” %\>  
\<% end %\>

On 2/5/06, matthew collins [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > >

---

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 6, 2006, 3:33am UTC](https://www.ruby-forum.com/t/render-partial-show-certain-content-only-if/101378/5 "2006-02-06T03:33:42Z")

</div>

matthew collins wrote:

> i had to go with having the link in the list.rhtml page only. the other  
> method, it would still show the word “link”, it just wouldn’t be linked.

Sorry, I should have realized that. The a spoonful eRB to the rescue:

# part of list.rhtml

\<%= render :partial =\> “post”, :locals =\> { :allow\_link =\> true } %\>

# now in \_post.rhtml

\<% if allow\_link %\>  
%= link\_to ‘link’, :action =\> ‘show’, :id =\> post %\> |  
\<% end %\>

This way the html won’t be rendered at all if you pass :allow\_link =\>  
false.

Jeff

> **[Hey Daddio!](http://www.softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 6, 2006, 2:58pm UTC](https://www.ruby-forum.com/t/render-partial-show-certain-content-only-if/101378/6 "2006-02-06T14:58:53Z")

</div>

You should be able to get this to a one liner:  
\<%= link\_to(“link”, :action =\> “show”, :id =\> post) if allow\_link %\>

Just gave it a quick try and it worked out.

---

<div class="post-metadata">

**Author:** ![matthew\_collins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@matthew\_collins](https://www.ruby-forum.com/u/matthew_collins)\
**Post date:** [February 5, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/render-partial-show-certain-content-only-if/101378/7 "2006-02-05T23:05:07Z")

</div>

in the view file?

doesn’t that violate some MVC philosophy?

just curious, not trying to troll or snark.
