ruby on rails - Routes with optional parameters -
following attempt seems functional, not 'clean' result trying archive.
following route
get "/learn(/:category)", to: "users#index", as: "learn"
should useable "/learn/technology" - works, if entered manually in address bar.
if tough try achieve similar in views, following: "/learn?category=technology" - well, technically works, not want.
i'm using following inside view:
- category.promoted.limit(7).each |category| %li.category-button = link_to learn_path(category) = button_tag "", :class => "#{category.name}" = content_tag(:span, category.to_s, :class => 'category-head')
and category model looks following:
class category < activerecord::base has_many :skills validates_uniqueness_of :name scope :promoted, lambda { where(:promoted => true) } def to_s read_attribute(:name).titleize end def to_param name.parameterize end end
how achieve 'cleaner' solution?
edit:
following works - there must better solution that?
get "/learn", to: "users#index", as: "learn" "/learn/:category", to: "users#index", as: "filter_learn"
try changing link following:
... = link_to learn_path(category: category.name) ...
Comments
Post a Comment