Forum: Ruby on Rails routing title in url

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
97296b682f921472a02639f7fad1f847?d=identicon&s=25 Kyle Jones (chillypepper)
on 2006-01-04 08:21
I've set up my app to display a post by retrieving its title instead of
id, which works fine, except it looks like this
"/post/the+title+of+post%3F" and I want it to be "the_title_of_post?"
with underscores instead of plusses.  Can anyone shed light?
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 Marcel Molina Jr. (Guest)
on 2006-01-04 08:38
(Received via mailing list)
On Wed, Jan 04, 2006 at 08:21:19AM +0100, Kyle Jones wrote:
> I've set up my app to display a post by retrieving its title instead of
> id, which works fine, except it looks like this
> "/post/the+title+of+post%3F" and I want it to be "the_title_of_post?"
> with underscores instead of plusses.  Can anyone shed light?

You could define Post#to_param like this:

  class Post < ActiveRecord::Base
    def to_param
      title.gsub(/\s+/, '_')
    end
  end

Then:

  link_to @post.title, :action => 'show', :id => @post

  or

  link_to @post.title, show_post_url(:id => @post)


to_param will be called implicitly.

marcel
97296b682f921472a02639f7fad1f847?d=identicon&s=25 Kyle Jones (chillypepper)
on 2006-01-04 09:01
That works at least to create the desired URL, but now i still have to
find_by_post with it, and it doesnt like the new underscores.  also how
would you retain characters like "?" instead of "%3F" ?
97296b682f921472a02639f7fad1f847?d=identicon&s=25 Kyle Jones (chillypepper)
on 2006-01-04 09:02
Kyle Jones wrote:
> That works at least to create the desired URL, but now i still have to
> find_by_post with it, and it doesnt like the new underscores.  also how
> would you retain characters like "?" instead of "%3F" ?

find_by_title my bad :-P
Bb3a34ec5060c2ab825f5be837561303?d=identicon&s=25 docpepin (Guest)
on 2006-12-30 09:20
Kyle Jones wrote:
> I've set up my app to display a post by retrieving its title instead of
> id, which works fine, except it looks like this
> "/post/the+title+of+post%3F" and I want it to be "the_title_of_post?"
> with underscores instead of plusses.  Can anyone shed light?

maybe the plugin acts_as_urlnameable from
http://gironda.org/acts_as_urlnameable/
could help you.
5a903f9189f9e232dead46eb6be1dd19?d=identicon&s=25 kris (Guest)
on 2006-12-30 09:33
(Received via mailing list)
or even acts_as_sluggable. http://dev.2750flesk.com/acts_as_sluggable/
This topic is locked and can not be replied to.