开发者

How to use parameterize in Rails?

开发者 https://www.devze.com 2022-12-11 05:43 出处:网络
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 inflect

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号