Routing in Rails with Unique Column Names
April 25, 2009
A small question popped up while I working with rails today, I’m not sure how well this is documented and it seems fairly straight forward but I feel the urge to share it.
The question is this: how can I route using, instead of a unique id, a different column in a set of data.
Say, for example I have a table with the columns id and name and both are unique. The data contained in my table is:
| id | name |
|---|---|
| 1 | Fred |
The default routing would be as such:
http://localhost/users/1
But what I am after is this:
http://localhost/Fred
How is this done? After playing around here is what I came up with:
Open the routes.rb file and add the line:
map.connect ':name', :controller => 'users', :action => 'show'
So we are passing the parameter :name to our show action in the user controller. Now we need to modify our users_controller.rb to accept this parameter.
def show
@user = User.find_by_name(params[:name])
end
And that’s all there is to it. Look up the Ruby on Rails API for more information on the find_by_ function
I haven’t had much experiance with ruby on rails so if I have said something that may be a little misleading make sure you let me know!
Getting off track with Rails!… Oh!, the puns!
April 18, 2009
Yes, like every other human being who has come across Ruby On Rails there is a strange temptation to start playing with words to show how clever we are but I’m fast to realise that its not the only thing you can do with Ruby On Rails, much to my relief.
If you have no idea what Ruby on Rails is see: http://rubyonrails.org.
Having been introduced to Rails many moons or perhaps about 2 years ago, it had always remained concealed behind a cloud of mystery for myself and as I developed websites for different clients using mainly PHP scripted Content Management Systems I didn’t feel the need to waste time and learn how to use it.
Silly me?
Perhaps so, though at some point there comes the urge to write a new system alot simpler than those systems such as Joomla! or others like it, a system that is tailor made to clients needs and not overloaded with features that have no relavance or are perhaps useless.
Time to removed that cloud of mystery … Enter Ruby on Rails.
Setting Up Rails
I currently am using Mac OS X Tiger 10.4 so I set out to find information on setting up Ruby On Rails.
A brilliant tutorial which has pretty much got me set was from hivelogic:
http://hivelogic.com/articles/view/ruby-rails-mongrel-mysql-osx
Though now with new updates and other current changes there was a few things that triped me up.
- In the Paths section of the tutorial you will see that you are to edit the .bash_login file so that terminal looks in our /usr/local directory though I found when I entered ‘ruby -v’ into terminal it was still showing my old 1.8.2 version of ruby. After scratching around some time on the internet I found that I would actually need to edit .bash_profile instead of .bash_login for terminal to recognise a new path.
- When installing the MySQL native bindings gem using the gem install method it was downloading yet failing to build, again I scratched around the net for a bit and found that due to Ruby Version 1.9.1 I would need to get the latest version of MySQL/Ruby and for some reason gems was only getting version 2.7 and the latest is 2.8 .1 (http://rubyforge.org/frs/?group_id=4550) so I downloaded that tar.gz file and installed it, tested and everything was looking great
I haven’t bothered installing a http server for Rails yet as I am just using the one packaged.
With everything installed, the fun begins…
More on Ruby On Rails comming soon!