Question

So before you say it, I have read through this http://guides.rubyonrails.org/routing.html and also I'm using Rails 3.2.13

I have defined wiki in my routes file as a resource and should therefore get the generated routing functions such as wiki_path() etc. I read in the above article that if a route function is defined, and you pass in an object, it can figure out the id based if it responds to the id method which all ActiveRecord classes do.

Here's my problem, my Wiki class does not inherit from ActiveRecord, and though I've defined the id function as below, the router is still passing in the entire wiki object as the id parameter.

def id 
  url_id = wiki.slug
  url_id ||= wiki.title
  url_id
end

The error I get is this when I try to create a new wiki:

ActionController::RoutingError at /wiki/what-is-the-meaning-of-life

No route matches {:action=>"show", :controller=>"wiki", 
:id=>#<Wiki:0x007f92a6793700 @user=nil, @path_to_repo="wiki.git", 
@wiki=#<Gollum::Wiki:70133917472560 wiki.git>, @page=nil, @persisted=false, 
  @attributes={"title"=>"what-is-the-meaning-of-life"}>}

I want the :id to be the return value of the id function in the Wiki class.

Was it helpful?

Solution

You need to_param method, not id:

def to_param 
  wiki.slug || wiki.title
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top