Created
June 23, 2010 08:19
-
-
Save josefrichter/449638 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [email protected]? && @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