matthew collins
2006-Feb-05 20:47 UTC
[Rails] render partial -- show certain content only if...
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 Cohen
2006-Feb-05 21:00 UTC
[Rails] Re: render partial -- show certain content only if...
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 -- Posted via http://www.ruby-forum.com/.
matthew collins
2006-Feb-05 21:50 UTC
[Rails] Re: render partial -- show certain content only if...
Jeff Cohen wrote:>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 > > >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
2006-Feb-05 21:54 UTC
[Rails] Re: render partial -- show certain content only if...
You can also do something like this: <% if params[:action] == "list" %> <%= render :partial => "poloroids" %> <%= render :partial => "demo" %> <% end %> On 2/5/06, matthew collins <matthewcollins@cfl.rr.com> wrote:> Jeff Cohen wrote: > > >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 > > > > > > > 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. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
matthew collins
2006-Feb-05 22:02 UTC
[Rails] Re: render partial -- show certain content only if...
in the view file? doesn''t that violate some MVC philosophy? just curious, not trying to troll or snark. Harm de Laat wrote:>You can also do something like this: > > ><% if params[:action] == "list" %> > <%= render :partial => "poloroids" %> > <%= render :partial => "demo" %> ><% end %> > > > > >On 2/5/06, matthew collins <matthewcollins@cfl.rr.com> wrote: > > >>Jeff Cohen wrote: >> >> >> >>>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 >>> >>> >>> >>> >>> >>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. >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Jeff Cohen
2006-Feb-06 02:33 UTC
[Rails] Re: Re: render partial -- show certain content only if...
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 -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Feb-06 13:56 UTC
[Rails] Re: Re: render partial -- show certain content only if...
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. On 2/5/06, Jeff Cohen <cohen.jeff@gmail.com> wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >