Wow, switching 开发者_StackOverflowto 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.
If you have more info to share about the business category beside just listing them all, you can use the 'show' of a business category
http://localhost:300/business_categories/1
If you want it to have a meaningful name instead of "1" (like the name of the category) you can use friendly_id plugin, that does just that :)
class BusinessCategory < ActiveRecord::Base
has_friendly_id :name, :use_slug => true
end
Then you can use http://localhost:300/business_categories/bbq
精彩评论