Rails form with a better URL
- by Sam
Wow, switching to REST is a different paradigm for sure and is mainly a headache right now.
view
<% form_tag (businesses_path, :method => "get") do %>
<%= select_tag :business_category_id, options_for_select(@business_categories.collect {|bc| [bc.name, bc.id ]}.insert(0, ["All Containers", 0]), which_business_category(@business_category) ), { :onchange => "this.form.submit();"} %>
<% end %>
controller
def index
@business_categories = BusinessCategory.find(:all)
if params[:business_category_id].to_i != 0
@business_category = BusinessCategory.find(params[:business_category_id])
@businesses = @business_category.businesses
else
@businesses = Business.all
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @businesses }
end
end
routes
map.resources
What I want to to is get a better URL than what this form is presenting which is the following:
http://localhost:3000/businesses?business_category_id=1
Without REST I would have do something like
http://localhost:3000/business/view/bbq bbq as permalink or I would have done http://localhost:300/business_categories/view/bbq and get the business that are associated with the category but I don't really know the best way of doing this.
So the two questions are what is the best logic of finding a business by its categories using the latter form and number two how to get that in a pretty URL all through RESTful routes in Rails.