Skip to content

Instantly share code, notes, and snippets.

@josefrichter
Created June 23, 2010 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefrichter/449638 to your computer and use it in GitHub Desktop.
Save josefrichter/449638 to your computer and use it in GitHub Desktop.
def create
@suggestion = Suggestion.new(params[:suggestion])
# create slug - TODO move somewhere
@suggestion.slug = params[:suggestion][:title].parameterize
# adjust slug if not unique
i=3
while !@suggestion.valid? && @suggestion.errors["slug"].include?("is already taken")
if @suggestion.slug[/-\d+$/].nil? # does the slug contain "-123" at the end?
@suggestion.slug<<"-2" # if not, assuming it's first duplicate and add "-2"
else
@suggestion.slug.gsub!(/-\d+$/,"-"+i.to_s) # replace "-2" with "-3" etc..
i+=1
end
end
respond_to do |format|
if @suggestion.save
format.html { redirect_to(@suggestion, :notice => 'Document was successfully created.') }
format.xml { render :xml => @suggestion, :status => :created, :location => @suggestion }
else
format.html { render :action => "new" }
format.xml { render :xml => @suggestion.errors, :status => :unprocessable_entity }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment