Hi I frequently use link_to in the context of conditional statement, What do you think about a link helper that return nil when the condition is false ex: # condtional = nil || false <%= link_to_if conditional, an_url, allow_hide: true %> # => nil or <%= link_to_if! conditional, an_url %> # => nil Thank you! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
What about using link_to_unless ? 2013/12/17 angelo capilleri <capilleri@gmail.com>> Hi > > I frequently use link_to in the context of conditional statement, > What do you think about a link helper that return nil when the condition > is false > ex: > > # condtional = nil || false > > <%= link_to_if conditional, an_url, allow_hide: true %> # => nil > > or > > <%= link_to_if! conditional, an_url %> # => nil > > Thank you! > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-core+unsubscribe@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/groups/opt_out. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
You mean like these? http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_unless On 17 Dec 2013, at 14:43, angelo capilleri <capilleri@gmail.com> wrote:> Hi > > I frequently use link_to in the context of conditional statement, > What do you think about a link helper that return nil when the condition is false > ex: > > # condtional = nil || false > > <%= link_to_if conditional, an_url, allow_hide: true %> # => nil > > or > > <%= link_to_if! conditional, an_url %> # => nil > > Thank you! > > > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/groups/opt_out.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
No, currently link_to_if(unless) returns the name in the false case. Il giorno martedì 17 dicembre 2013 15:52:57 UTC+1, Zamith ha scritto:> > You mean like these? > > http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if > http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_unless > > On 17 Dec 2013, at 14:43, angelo capilleri <capi...@gmail.com<javascript:>> > wrote: > > Hi > > I frequently use link_to in the context of conditional statement, > What do you think about a link helper that return nil when the condition > is false > ex: > > # condtional = nil || false > > <%= link_to_if conditional, an_url, allow_hide: true %> # => nil > > or > > <%= link_to_if! conditional, an_url %> # => nil > > Thank you! > > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-co...@googlegroups.com <javascript:>. > To post to this group, send email to rubyonra...@googlegroups.com<javascript:> > . > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Angelo, thank you for your suggestion, but is not this already possible using a normal `if` statement? ```ruby <% if conditional %> <%= link_to ... %> <% end %> ``` I see a point to having a helper if you use this same pattern in a lot of places. But, for this case, I recommend to define a helper in your application. Rafael Mendonça França http://twitter.com/rafaelfranca https://github.com/rafaelfranca On Tue, Dec 17, 2013 at 12:43 PM, angelo capilleri <capilleri@gmail.com>wrote:> Hi > > I frequently use link_to in the context of conditional statement, > What do you think about a link helper that return nil when the condition > is false > ex: > > # condtional = nil || false > > <%= link_to_if conditional, an_url, allow_hide: true %> # => nil > > or > > <%= link_to_if! conditional, an_url %> # => nil > > Thank you! > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-core+unsubscribe@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/groups/opt_out. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Sure, the purpose of my example above is only to introduce this. When you have a sidebar partial with 3 or more dinamic menu, ex: ```ruby <% if user_role.admin? %> <%= link_to an_url,.. do %> Menu Label for admin <% end %> <% end %> <% if user_role.user?%> <%= link_to an_url,.. do %> Menu Label for user <% end %> <% end %> <% if user_role.guest?%> <%= link_to an_url,.. do %> Menu Label for gues <% end %> <% end %> ``` Whit this helper we can have the following code: ```ruby <%= link_to_if! user_role.admin? an_url,.. do %> Menu Label for admin <% end %> <%= link_to_if! user_role.user? an_url,.. do %> Menu Label for user <% end %> <%= link_to_if! role.guest?, an_url,.. do %> Menu Label for guest <% end %> ``` In other word has the same role of link_to_if(unless) but allows to get to not display nothing in the conditional false case Il giorno martedì 17 dicembre 2013 16:11:34 UTC+1, Rafael Mendonça França ha scritto:> > Angelo, > > thank you for your suggestion, but is not this already possible using a > normal `if` statement? > > ```ruby > <% if conditional %> > <%= link_to ... %> > <% end %> > ``` > > I see a point to having a helper if you use this same pattern in a lot of > places. But, for this case, I recommend to define a helper in your > application. > > Rafael Mendonça França > http://twitter.com/rafaelfranca > https://github.com/rafaelfranca > > > On Tue, Dec 17, 2013 at 12:43 PM, angelo capilleri <capi...@gmail.com<javascript:> > > wrote: > >> Hi >> >> I frequently use link_to in the context of conditional statement, >> What do you think about a link helper that return nil when the condition >> is false >> ex: >> >> # condtional = nil || false >> >> <%= link_to_if conditional, an_url, allow_hide: true %> # => nil >> >> or >> >> <%= link_to_if! conditional, an_url %> # => nil >> >> Thank you! >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Core" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to rubyonrails-co...@googlegroups.com <javascript:>. >> To post to this group, send email to rubyonra...@googlegroups.com<javascript:> >> . >> Visit this group at http://groups.google.com/group/rubyonrails-core. >> For more options, visit https://groups.google.com/groups/opt_out. >> > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
Maybe would be better if you continue to do what you are doing or define a helper in your side. For your use case you may have a <div> that can’t be seem by a normal users, so link_to_if! will not help you, and I don’t see us adding content_for_if or form_tag_if inside Rails. Rafael Mendonça França http://twitter.com/rafaelfranca https://github.com/rafaelfranca On Tue, Dec 17, 2013 at 1:34 PM, angelo capilleri <capilleri@gmail.com>wrote:> Sure, > the purpose of my example above is only to introduce this. > > When you have a sidebar partial with 3 or more dinamic menu, ex: > > ```ruby > <% if user_role.admin? %> > <%= link_to an_url,.. do %> > Menu Label for admin > <% end %> > <% end %> > > <% if user_role.user?%> > <%= link_to an_url,.. do %> > Menu Label for user > <% end %> > <% end %> > > <% if user_role.guest?%> > <%= link_to an_url,.. do %> > Menu Label for gues > <% end %> > <% end %> > ``` > Whit this helper we can have the following code: > > ```ruby > <%= link_to_if! user_role.admin? an_url,.. do %> > Menu Label for admin > <% end %> > > <%= link_to_if! user_role.user? an_url,.. do %> > Menu Label for user > <% end %> > > <%= link_to_if! role.guest?, an_url,.. do %> > Menu Label for guest > <% end %> > ``` > In other word has the same role of link_to_if(unless) but allows to get to > not display nothing in the conditional false case > > Il giorno martedì 17 dicembre 2013 16:11:34 UTC+1, Rafael Mendonça França > ha scritto: >> >> Angelo, >> >> thank you for your suggestion, but is not this already possible using a >> normal `if` statement? >> >> ```ruby >> <% if conditional %> >> <%= link_to ... %> >> <% end %> >> ``` >> >> I see a point to having a helper if you use this same pattern in a lot of >> places. But, for this case, I recommend to define a helper in your >> application. >> >> Rafael Mendonça França >> http://twitter.com/rafaelfranca >> https://github.com/rafaelfranca >> >> >> On Tue, Dec 17, 2013 at 12:43 PM, angelo capilleri <capi...@gmail.com>wrote: >> >>> Hi >>> >>> I frequently use link_to in the context of conditional statement, >>> What do you think about a link helper that return nil when the condition >>> is false >>> ex: >>> >>> # condtional = nil || false >>> >>> <%= link_to_if conditional, an_url, allow_hide: true %> # => nil >>> >>> or >>> >>> <%= link_to_if! conditional, an_url %> # => nil >>> >>> Thank you! >>> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Ruby on Rails: Core" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to rubyonrails-co...@googlegroups.com. >>> To post to this group, send email to rubyonra...@googlegroups.com. >>> >>> Visit this group at http://groups.google.com/group/rubyonrails-core. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >> >> -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-core+unsubscribe@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/groups/opt_out. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
You can already achieve basically the same functionality with Ruby and conditional modifiers: <%= link_to an_url,.. do %> Menu Label for admin <% end if user_role.admin? %> <%= link_to an_url,.. do %> Menu Label for user <% end if user_role.user? %> <%= link_to an_url,.. do %> Menu Label for gues <% end if user_role.guest? %> I don''t think there''s need for new methods in this scenario. Thanks! <3 On Tue, Dec 17, 2013 at 1:34 PM, angelo capilleri <capilleri@gmail.com>wrote:> Sure, > the purpose of my example above is only to introduce this. > > When you have a sidebar partial with 3 or more dinamic menu, ex: > > ```ruby > <% if user_role.admin? %> > <%= link_to an_url,.. do %> > Menu Label for admin > <% end %> > <% end %> > > <% if user_role.user?%> > <%= link_to an_url,.. do %> > Menu Label for user > <% end %> > <% end %> > > <% if user_role.guest?%> > <%= link_to an_url,.. do %> > Menu Label for gues > <% end %> > <% end %> > ``` > Whit this helper we can have the following code: > > ```ruby > <%= link_to_if! user_role.admin? an_url,.. do %> > Menu Label for admin > <% end %> > > <%= link_to_if! user_role.user? an_url,.. do %> > Menu Label for user > <% end %> > > <%= link_to_if! role.guest?, an_url,.. do %> > Menu Label for guest > <% end %> > ``` > In other word has the same role of link_to_if(unless) but allows to get to > not display nothing in the conditional false case > > Il giorno martedì 17 dicembre 2013 16:11:34 UTC+1, Rafael Mendonça França > ha scritto: >> >> Angelo, >> >> thank you for your suggestion, but is not this already possible using a >> normal `if` statement? >> >> ```ruby >> <% if conditional %> >> <%= link_to ... %> >> <% end %> >> ``` >> >> I see a point to having a helper if you use this same pattern in a lot of >> places. But, for this case, I recommend to define a helper in your >> application. >> >> Rafael Mendonça França >> http://twitter.com/rafaelfranca >> https://github.com/rafaelfranca >> >> >> On Tue, Dec 17, 2013 at 12:43 PM, angelo capilleri <capi...@gmail.com>wrote: >> >>> Hi >>> >>> I frequently use link_to in the context of conditional statement, >>> What do you think about a link helper that return nil when the condition >>> is false >>> ex: >>> >>> # condtional = nil || false >>> >>> <%= link_to_if conditional, an_url, allow_hide: true %> # => nil >>> >>> or >>> >>> <%= link_to_if! conditional, an_url %> # => nil >>> >>> Thank you! >>> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Ruby on Rails: Core" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to rubyonrails-co...@googlegroups.com. >>> To post to this group, send email to rubyonra...@googlegroups.com. >>> >>> Visit this group at http://groups.google.com/group/rubyonrails-core. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >> >> -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-core+unsubscribe@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/groups/opt_out. >-- At. Carlos Antonio -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.
On 17-Dec-2013, at 9:11 pm, Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> wrote:> <%= link_to an_url,.. do %> > Menu Label for admin > <% end if user_role.admin? %>Although it doesn’t change things much, I would write this as: <%= link_to “Menu Label for Admin”, a_url if user_role.admin? %> This is mostly because I can’t wrap my head around do..end..if, and it looks shorter. I’ll admit that link_to_if is a bit confusing the first time you encounter it, but i got used to it after a while. I expected link_to_if to literally be: `link_to(*args) if condition’. That being said, I’ve never actually used link_to_if, because I haven’t found a use case for it personally. -- Tejas Dinkar http://www.nilenso.com Nilenso Software (formerly C42 Engineering) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/groups/opt_out.