Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use parameterize in Rails?

I want to convert the title of a page to a friendly URL and store it in the database as a permalink. My problem is I can't use the parameterize method. It's not working. Other inflections are working like upcase or downcase but parameterize is not working. Is there a special case for parameterize?

This is my code:

Controller:

def create
 params[:page][:permalink] = params[:page][:title].dup
 @page = Page.new(params[:page])
end

Model:

class Page < ActiveRecord::Base
 before_save :makeitpermalink
 before_update :makeitpermalink

 private
  def makeitpermalink
    permalink.parameterize!
  end
end
like image 226
railslover Avatar asked Nov 06 '09 17:11

railslover


1 Answers

According to the Rails' documentation, there is no bang (exclamation mark) version of the parameterize method, so try removing it:

def make_it_permalink
  self.permalink = self.permalink.parameterize
end
like image 100
John Topley Avatar answered Sep 16 '22 11:09

John Topley