Forum: Ruby on Rails Routing Requests with a database field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6de7ba135902e959c4c731f6afa0f40d?d=identicon&s=25 matt.ramos (Guest)
on 2005-11-16 04:24
(Received via mailing list)
Hello.

I am building a website that has a growing list of projects. So lets say
I'd
like to go to http://localhost:3000/project/submarine/ and view the
"Submarine Project". I'd have a database, and an entry could be. id=1,
title=submarine, description=we built a submarine. I've been reading
through
the pragprog rails book and asking in the #rubyonrails irc channel, but
I
can't find the answer anywhere. I couldn't even find anything on google.

Any help is greatly appreciated.

Good day,
Matt
2585e5de3d03793e39077e3d8b73dfaf?d=identicon&s=25 ronnylist (Guest)
on 2005-11-16 04:42
(Received via mailing list)
On Tue, Nov 15, 2005 at 10:21:42PM -0500, Matt Ramos wrote:
> I am building a website that has a growing list of projects. So lets
> say I'd like to go to http://localhost:3000/project/submarine/ and
> view the "Submarine Project". I'd have a database, and an entry
> could be. id=1, title=submarine, description=we built a submarine.
> I've been reading through the pragprog rails book and asking in the
> #rubyonrails irc channel, but I can't find the answer anywhere. I
> couldn't even find anything on google.

You might be interested in Named Routes.
http://wiki.rubyonrails.com/rails/pages/NamedRoutes

In config/routes.rb:

    map.project 'project/:projtitle' :controller => 'project',
				     :action => 'show'

In app/controllers/project_controller.rb:

    def show
      @project = Project.find(:first,
      			      :conditions => ['title = ?',params[:projtitle]])
      ...
    end

Or something like that. Assuming your project titles are unique. Then
you can also use project_url (since you have map.project in routes.rb)
for urls in your views, e.g.

    <% for project in @projects %>
    <%= link_to project.title,
                project_url(:projtitle => project.title) %>
     ...
    <% end %>

Named Routes is a really cool feature, and one of the best of Rails,
I'd say.

Ronny
6de7ba135902e959c4c731f6afa0f40d?d=identicon&s=25 matt.ramos (Guest)
on 2005-11-16 04:54
(Received via mailing list)
Thanks a lot. I'll give that a shot tomorrow sometime, and I'll probably
post back because I know absolutely nothing.
This topic is locked and can not be replied to.