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):
1 个解决方案
#1
3
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
.
我将发布一个答案,尽管上面的注释给出了答案:CanCan干扰to_param——解决方案是授权控制器中的每个资源,而不是使用常规的load_and_authorize_resources。
#1
3
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
.
我将发布一个答案,尽管上面的注释给出了答案:CanCan干扰to_param——解决方案是授权控制器中的每个资源,而不是使用常规的load_and_authorize_resources。