I've got some ugly code I don't know how to improve on. I've got a
link_to tag that takes a fair amount of html as a block. I need the
href value for the link to be different depending on some logic, there
are four possible values for the href.

What I ended us with was this, and it's pretty unreadable. Any
suggestions on how to do this better?

<%= link_to (@brand.nil? ? (@store.nil? ?
(from_catalog_path(params[:id].blank? ? Category.root.id :
params[:id], product.id, product.name_url)) :
(from_store_path(@store.id, product.id))) :
(from_brand_path(@brand.id, product.id))) do -%>
...... HTML IN HERE .....
<% end %>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Tim Shaffer at Feb 21, 2012 at 2:52 pm
    Maybe just put the logic in the controller?

    if @brand.nil?
    if @store.nil?
    @brand_link_path = from_catalog_path(params[:id].blank? ?
    Category.root.id : params[:id], product.id, product.name_url)
    else
    @brand_link_path = from_store_path(@store.id, product.id)
    end
    else
    @brand_link_path = from_brand_path(@brand.id, product.id)
    end

    Then it simplifies your view:

    <%= link_to(@brand_linnk_path) do -%> HTML HERE <% end %>

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/dzgOPAlRz7EJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 21, '12 at 2:36p
activeFeb 21, '12 at 2:52p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

John SB: 1 post Tim Shaffer: 1 post

People

Translate

site design / logo © 2021 Grokbase