Rails URL helper for sorting filters -
i have table companies field city_id. need in sorting companies cities. next:
# companies_controller.rb def sort_by_city @cities = city.joins(:company).uniq @companies = company.where("city_id = ?", params[:city_id]) render 'index' end # routes.rb: '/companies/city/:city_id', to: 'companies#sort_by_city'
and in index.html.erb try make links filter:
<% @cities.each |city| %> <li><%= link_to city.name, unknown_path(city.id) %> (<%= city.companies_count %>) <% end %>
i want links this: "www.site.com/companies/city/23" must write instead of unknown_path? or wrong before (in controller or in routes.rb)?
you can give name route as
option :
for example :
get '/companies/city/:city_id', to: 'companies#sort_by_city', as: 'companies_city_sort'
and use
companies_city_sort_path
also, have @ resourceful routing, may more adapted.
Comments
Post a Comment