开发者

Rails Friendly URLs

I have a model called Project and I've defined the "to_param" method as this:

def to_param
  permalink
end

I am using this permalink plugin github.com/febuiles/make_permalink so when I call p.permalink it will gen开发者_JS百科erate id-name.

The problem is that when I TYPE /projects/1 in my web-browser the URL doesn't change to /projects/1-name. How can I achieve that?

Look that when I go to http://railscasts.com/episodes/63 the URL changes to /63-name

Any suggestions?

Thanks in advance


It looks like Ryan is using a conditional redirect to the proper episode_url. The Railscasts application source code is on GitHub, so you can see how he does it.

Similarly as Drew suggests, this should work fine in your case.

redirect_to @project, :status => 301 if params[:id] != @project.to_param?


They are most likely using the friendly_id gem or something similar:

http://github.com/norman/friendly_id/

The important part is:

@post = Post.find(params[:id])
redirect_to @post, :status => 301 if @post.has_better_id?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜