Forum: Ruby on Rails String manipulation

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.
Dan (Guest)
on 2006-03-24 18:33
I need to make some nicenames from my post titles, like:
- convert spaces to "-"
- delete all the !,?,commas,.,;.:,* from the string

How can I do? Sorry for the noob question...

Oh, and another thing... How can I make permalink this way:
Not .../blog/show/4 but .../category/nicename ?


Thankyou.
James Ludlow (Guest)
on 2006-03-24 18:40
(Received via mailing list)
On 3/24/06, Dan <ljundberg_83@yahoo.it> wrote:
> I need to make some nicenames from my post titles, like:
> - convert spaces to "-"
> - delete all the !,?,commas,.,;.:,* from the string
>
> How can I do? Sorry for the noob question...

Lookup the gsub! method in the String documentation.  (Look in the
Ruby docs, not the Rails docs.)

> Oh, and another thing... How can I make permalink this way:
> Not .../blog/show/4 but .../category/nicename ?

http://manuals.rubyonrails.com/read/chapter/65
Jens Kraemer (Guest)
on 2006-03-24 18:42
(Received via mailing list)
On Fri, Mar 24, 2006 at 06:33:37PM +0100, Dan wrote:
> I need to make some nicenames from my post titles, like:
> - convert spaces to "-"
> - delete all the !,?,commas,.,;.:,* from the string
>
> How can I do? Sorry for the noob question...
>
> Oh, and another thing... How can I make permalink this way:
> Not .../blog/show/4 but .../category/nicename ?

you should have a look at the typo code (typo.leetsoft.com). Imho it
solves both issues you mentioned.

Regards,
Jens
Frank (Guest)
on 2006-03-24 20:53
(Received via mailing list)
Hi Dan,

  >> I need to make some nicenames from my post titles, like:
  >> - convert spaces to "-"
  >> - delete all the !,?,commas,.,;.:,* from the string

  Try something like:

  delete all the characters: (Add, or modify characters as needed)

  link=@article[:title].gsub(/(['*&^%$+_))((**&&^%$\#@\!~`{}\/\.\,\;])/,
'' )

  create permalink:

  [root@srv31 ror]# ruby script/console
  Loading development environment.
  str=>> str="My long string"
  => "My long string"
  >>   def create_permalink(string)
  >>     string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
  ?>       '%' + $1.unpack('H2' * $1.size).join('%').upcase
  >>      end.tr(' ', '-').downcase
  >>
  ?>   end
  => nil
  >> permalink=create_permalink(str)
  => "my-long-string"
  >>

  Modify to your preferences.

  >>Oh, and another thing... How can I make permalink this way:
  >> Not .../blog/show/4 but .../category/nicename ?

  Try something like this in routes.rb

  map.connect 'category/:link', :controller => "blog", :action => 'show'

  Hope this helps.

  Frank

Dan <ljundberg_83@yahoo.it> wrote:



Want to read my Pro Rails book or Pro Server Management book? Become a
beta reader now. Write me to learn more.

Rails Blog: http://railsruby.blogspot.com
MySQL Blog: http://mysqldatabaseadministration.blogspot.com
Linux / Security Blog: http://frankmash.blogspot.com
Programming One Liners: http://programming-oneliners.blogspot.com
This topic is locked and can not be replied to.