March 01, 2015

Home » » How To Build - Rewrite Url On Rails

How To Build - Rewrite Url On Rails

Posted by Unknown
0

Install gem stringex

Add following lines to Gemfile
gem 'stringex', '~> 2.5.2'
gem 'responders'
Run command
# bundle install

Rails application have model Post with id:integer and title:string

class Post < ActiveRecord::Base
  acts_as_url :title, url_attribute: :slug
 
  def to_param
    "#{id}-#{slug}"
  end
end

With config above, example: I have a post with id = 1 and title = "This is an example" then link to post look like

http://localhost:3000/posts/1-this-is-an-example






0 comments:

Post a Comment

Popular Posts

Labels

Archive

 

Blogroll

Recepies

Flickr Images

Copyright © 2014. Tutorials Blog - All Rights Reserved