Forum: Ruby on Rails render partial -- show certain content only if...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
matthew collins (Guest)
on 2006-02-05 22:49
(Received via mailing list)
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
Jeff C. (Guest)
on 2006-02-05 23:00
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
matthew collins (Guest)
on 2006-02-05 23:53
(Received via mailing list)
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.
Harm de Laat (Guest)
on 2006-02-05 23:56
(Received via mailing list)
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:
> >>
matthew collins (Guest)
on 2006-02-06 00:05
(Received via mailing list)
in the view file?

doesn't that violate some MVC philosophy?

just curious, not trying to troll or snark.
Jeff C. (Guest)
on 2006-02-06 04:33
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
Nick S. (Guest)
on 2006-02-06 15:58
(Received via mailing list)
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.
This topic is locked and can not be replied to.