views:

336

answers:

2

I have an admin namespace which gives me the usual routes such as admin_projects and admin_project, however they are not behaving in the usual way. This is my first Rails 2.3 project so maybe related I can't find any info via Google however.

map.namespace(:admin) do |admin|
  admin.resources :projects
end

The strange thing is for a given URL (eg. /admin/projects/1) I don't have to pass in an object to get URL's it somehow guesses them:

<%= admin_project_path %> # => /admin/projects/1

No worries, not really a problem just not noticed this before.

But if I try and pass an object as is usual:

<%= admin_project_path(@project) %> # => admin_project_url failed to generate from {:controller=>"admin/projects", :action=>"show", :id=>#<Project id: 1, name: "teamc...>

":id" seems to contain the entire object, so I try passing the id directly and it works:

<%= admin_project_path(@project.id) %> # => /admin/projects/1

This would not be so bad but when it comes to forms I usually use [:admin, @object], however:

<%= url_for [:admin, @project.id] %> # => undefined method `admin_fixnum_path'

So I can't pass in an id, as it needs an objects class to work out the correct route to use.

<%= url_for [:admin, @project] %> # => Unfortunately this yields the same error as passing a object to admin_project_path, which is what it is calling.

I can't spot any mistakes and this is pretty standard so I'm not sure what is going wrong...

+1  A: 

Interesting. What happens when you define a to_param method on Project? For instance

class Project < ActiveRecord::Base
  def to_param
    self.id
  end
end

This should be the default and this shouldnt be necessary. But what happens if you make it explicit? Try adding the above method then going back to your original approach of only passing around @project

Cody Caughlan
This was the problem, I had 'to_param' set to return the projects unique slug. However the slug was always nil (my bad).
Kris
A: 

I wish I could help you on this one. We have a large application with several namespaced sections. Our routes are defined in the exact method you have described and we are calling our path helper with objects. No where in the application are we accessing using the id.

Our application started on Rails 2.1 and has transitioned through 2.2 and 2.3 with no significant changes to the routing. Sorry I couldn't be more help.

Peer

Peer Allan