As an exercise for the reader, comment your three map statements in
routes.rb, then run a rake routes (I usually route that output to a file
for easier reading). At first glance, you shouldn't need any of your
custom maps.
@polomasta: You need to read up on how Rails does its route matching...
maybe it's as easy as changing the controller code to check if the :id
parameter is an int, perform a Project.find(:params[:id]), otherwise
give the name based search a go Project.find_by_name(params[:id]) <-
doesn't really matter what the param is called
As an exercise for the reader, comment your three map statements in
routes.rb, then run a rake routes (I usually route that output to a file
for easier reading). At first glance, you shouldn't need any of your
custom maps.
Ok so on removing my custom map routes and raking I get the following:
(in /home/webbie/peoplesearch)
users GET /users
{:controller=>"users", :action=>"index"}
formatted_users GET /users.:format
{:controller=>"users", :action=>"index"}
POST /users
{:controller=>"users", :action=>"create"}
POST /users.:format
{:controller=>"users", :action=>"create"}
new_user GET /users/new
{:controller=>"users", :action=>"new"}
formatted_new_user GET /users/new.:format
{:controller=>"users", :action=>"new"}
edit_user GET /users/:id/edit
{:controller=>"users", :action=>"edit"}
formatted_edit_user GET /users/:id/edit.:format
{:controller=>"users", :action=>"edit"}
user GET /users/:id
{:controller=>"users", :action=>"show"}
formatted_user GET /users/:id.:format
{:controller=>"users", :action=>"show"}
PUT /users/:id
{:controller=>"users", :action=>"update"}
PUT /users/:id.:format
{:controller=>"users", :action=>"update"}
DELETE /users/:id
{:controller=>"users", :action=>"destroy"}
DELETE /users/:id.:format
{:controller=>"users", :action=>"destroy"}
This looks ok to me (Like that means anything ) but It still renders
the routes in exactly the same fashion.
user_path(user) and edit_user_path(user) both appear to render nothing
at all. Rather than /appname/id_no/ and /appname/id_no/edit/
Is there perhaps something I need to set up in another config file.
Annoyingly most of the documentation out there appears to be for rails
1.2 and not 2.0
Hmm... could you post your index.html.erb (or whatever) code?
Ok sure index.html.erb is
<h1>Listing users</h1>
<table>
<tr>
</tr>
<% for user in @users %>
<tr>
<td><%= h user.ID %></td>
<td><%= h user.SurName %></td>
<td><%= h user.ForeName %></td>
<td><%= h user.Title %></td>
<td><%= h user.RoomNo %></td>
<td><%= h user.PhoneNo %></td>
<td><%= h user.EMail %></td>
Dang... that looks pretty vanilla to me. What about the page source
from the browser? edit_user_path and user_path are just helpers that
create the href's... and should be equivalent to you typing in something
like:
Interestingly if I put that code in, the page fails as user.id doesn't
exist, I think I've messed something up in the initial app set-up with
my "./script/generate scaffold Users" command.
I shall delete the app and start again from scratch I think.
I've been reading everything about routes but I can't seem to make
this work..
I have a projects controller and my projects table contains id and
name fields
I want to be able to navigate to myurl.com/projects/"name" instead of
the default method or /projects/"id"
Seems like this should be pretty basic but I haven't been able to get
it to work.
Appreciate the help.
The most common way to do this is to override the to_param method on
your model. When rails generates a route for a model it calls to_param
on that model object, which ends up in the :id spot on your url. By
default, this returns the id.
If you add something like this to your model, where permalink is a
database field on that model:
def to_param
permalink
end
Then when you generate routes for that model object it will look like:
/thingies/my_cool_permalink
Now you just need to change how you fetch your models. On your show
action, change the standard