Slick23
Slick23

Reputation: 5897

Problem with "to_param" in rails

I have a page model that I want to find by a handle that I create from the title.

class Page < ActiveRecord::Base
  belongs_to :user

  validates_presence_of :title

  def to_param
    handle
  end

  def self.make_url_safe(string)
    handle = string.titleize.gsub(/ /,'').underscore.dasherize[0..35] 
    "#{handle}/" 
  end

end

In my controller, I have:

def show
    @page = Page.find_by_handle(params[:id])
 end

I'm doing the same thing with another model and it's working fine, but not with Page. I keep getting this error:

ActiveRecord::RecordNotFound in
PagesController#show

Couldn't find Page with ID=test

Where test is the handle of the page. I feel like it was working just a few days ago when I created the model, so not sure what could've changed to cause the problem. Maybe the trailing /?

Here's the log:

Started GET "/pages/test" for 127.0.0.1 at 2011-05-28 11:53:49 -0400
  Processing by PagesController#show as HTML
  Parameters: {"id"=>"test"}
  Page Load (0.2ms)  SELECT "pages".* FROM "pages" WHERE ("pages"."id" = 0) LIMIT 1
Completed   in 12ms

ActiveRecord::RecordNotFound (Couldn't find Page with ID=test):

Upvotes: 3

Views: 719

Answers (1)

Slick23
Slick23

Reputation: 5897

I'll post an answer, though it was given in the comment above: CanCan interferes with to_param -- the solution is to authorize each resource in the controller, instead of using the generic load_and_authorize_resources.

Upvotes: 3

Related Questions