Render partial -- show certain content only if


#1

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


#2

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
www.softiesonrails.com


#3

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

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.


#4

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 wrote:


#5

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
www.softiesonrails.com


#6

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.


#7

in the view file?

doesn’t that violate some MVC philosophy?

just curious, not trying to troll or snark.