I cant understand my this is not working. I am trying to add a method to create a slug from the title of a page when the user creates a new page (using Ruby on Rails).
This is my create method:
def create
@page = Page.new(params[:page])
@page.user_id = current_user.id
@page.slug = @page.title.to_slug
respond_to do |format|
if @page.save
format.html { redirect_to @page, notice: 'Page was successfully created.' }
format.json { render json: @page, status: :created, location: @page }
else
format.html { render action: "new" }
format.json { render json: @page.errors, status: :unprocessable_entity }
end
end
end
Adn this is my method at the bottom of the same same controller. (pages_controller.rb) :
def to_slug(param=self.slug)
# strip the string
ret = param.strip
#blow away apostrophes
ret.gsub! /['`]/, ""
# @ --> at, and & --> and
ret.gsub! /\s*@\s*/, " at "
ret.gsub! /\s*&\s*/, " and "
# replace all non alphanumeric, periods with dash
ret.gsub! /\s*[^A-Za-z0-9\.]\s*/, '-'
# replace underscore with dash
ret.gsub! /[-_]{2,}/, '-'
# convert double dashes to single
ret.gsub! /-+/, "-"
# strip off leading/trailing dash
ret.gsub! /\A[-\.]+|[-\.]+\z/, ""
ret
end
The slug method is from this question: Best way to generate slugs (human-readable IDs) in Rails
I cant understand why this creates a method undefined error or how to fix it. Any help, hint or answer is appreciated. :)