koloa
February 6, 2007, 9:04pm
1
id like to have my site organized like this…
nj.com/southern/restaurants/italian/3
to have a url like that, it seems i would need a controller called
southern with an internal controller restaurants where 3 is the id of
the item.
thoughts? suggestions? thanks.
koloa
February 6, 2007, 9:21pm
2
That’s a perfectly valid URL, but it won’t mean
‘controller-in-controller’.
Rails routes are incredibly powerful, try something like this (near the
bottom to allow for other custom routes to take precendence).
map.connect ‘:region/:controller/:action/:id’
Then your :controller/:action will get:
params = {
:region => ‘southern’,
:id => 3
}
Jason
koloa
February 6, 2007, 9:55pm
3
On 2/6/07, koloa [email protected] wrote:
the item.
thoughts? suggestions? thanks.
I’ve been trying to make my URLs even more search engine friendly
lately. I’ve gotten away from using integer values in the URL for
anything except the admin/non-public side of my applications. I’ve
been using a :base_name column combined that with some routing.
In the model I do:
before_save :base_name_from_title
def base_name_from_title
self.base_name = title.downcase.gsub( /\ and\ /, ‘-’ ).
gsub( /\ on\ /, ‘-’ ).
gsub( /[^a-zA-Z0-9-]/, ‘-’ ).
gsub( /[-]+/, ‘-’ ).
gsub( /[-]$/, ‘’ ).
gsub( /^[-]/, ‘’ )
end
“title” is whatever important wordy field I can pick out of a given
model. It’s tough to pick sometimes especially if the field isn’t
unique. I don’t have too many collisions on my personal blog though.
Then I add a route:
map.connect “category/:base_name”,
:controller => ‘category’,
:action => ‘index’,
:requirements => { :base_name => /[\w-]+/ },
:base_name => nil
Then in the controller I look it up by base_name instead of by id:
Category.find( :first, :conditions => [ ‘base_name = ?’,
params[:base_name] ] )
Don’t forget to index that text field!
Then my URLs look like:
http://destiney.com/category/programming-languages-ruby
–
Greg D.
http://destiney.com/
koloa
February 6, 2007, 10:34pm
4
hi Jason and Greg, Thanks for the post…question though…
lets say my category field in a model can be ‘bbq’, ‘chinese’,
‘italian’…and id like to have the url
localhost.com/nj/bbq
or
localhost.com/nj/southern/bbq
where nj is a controller, southern is a :region field and bbq is a
:mcategory field,both are in my restaurant table…
do i define like this?
map.connect ‘:controller/:region/:mcategory’, :action => ‘view’
where view does the logic to determine what to pass in my find statement
like
def view
NJ.find(:all, :conditions => "mcategory = ? and region =
?"params[:mcategory, params [:region]])
end
the name ‘view’, will that be part of the url? i would prefer to have it
like localhost.com/nj/southern/bbq …
thanks.
Greg, id really like the way you did your titles.
Jason R. wrote:
That’s a perfectly valid URL, but it won’t mean
‘controller-in-controller’.
Rails routes are incredibly powerful, try something like this (near the
bottom to allow for other custom routes to take precendence).
map.connect ‘:region/:controller/:action/:id’
Then your :controller/:action will get:
params = {
:region => ‘southern’,
:id => 3
}
Jason
koloa
February 6, 2007, 10:43pm
5
whoa…i just tried it and it works and I can have the url that i like!
wow that feature is really awesome… i was about to brute force like so
many controllers within each other.
now i have to rethink my naming conventions of functions. my brain
hurts… thanks all for helping a noob.
koloa
February 6, 2007, 11:06pm
6
another quick question…
i used to have a long case stament like this…
switch on 'food ’ category
case ‘bbq’
…
if state == nj
do find all in nj for bbq
if state == pa
do find etc…
case ‘chinese’
…
if…
…
case …
end
switch on ‘something else’ category
…
…
now because of the routes.rb power, i no longer need my case statement
since i can have…
map.connect ‘:state, :main_category, :sub_category’
so when presses a link on my site, it can go to a action that just has
this?
def getView
@myObj = params;[:main_category].capitalize.find(:all, :conditions
[‘state = ? and subcategory = ?’, params[:state],
params[:sub_category]])
end
Thansk!
koloa
February 6, 2007, 11:30pm
7
Hey Jason, Thanks for explaining this to me! i think i am now going to
rewrite my app again for the third time cutting the code i have maybe by
30%!
Jason R. wrote:
Yep, it would work pretty much like that. Anything on the URL gets split
up
according to route definitions and placed into params.
Jason
koloa
February 6, 2007, 11:28pm
8
Yep, it would work pretty much like that. Anything on the URL gets split
up
according to route definitions and placed into params.
Jason