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 paramete开发者_开发百科rize
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
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
精彩评论