Skip to content

Instantly share code, notes, and snippets.

@davidlfox
Created June 19, 2014 01:27
Show Gist options
  • Save davidlfox/089edbd867b909b80738 to your computer and use it in GitHub Desktop.
Save davidlfox/089edbd867b909b80738 to your computer and use it in GitHub Desktop.
rails validation error with decimal
class Chore < ActiveRecord::Base
belongs_to :user
belongs_to :category
validates :price, :format => { :with => /\A\d{1,9}\.\d{0,2}\z/, :message => 'regex failed'}, :numericality => {:greater_than => 0}
validates :category, presence: true
validates :user, presence: true
end
# GET /chores/1/edit
def edit
@categories = Category.where(:is_active => true)
end
# PATCH/PUT /chores/1
# PATCH/PUT /chores/1.json
def update
respond_to do |format|
if @chore.valid? && @chore.update(chore_params)
format.html { redirect_to @chore, notice: 'Chore was successfully updated.' }
format.json { head :no_content }
else
logger.info @chore.errors.messages
@categories = Category.where :is_active == true
format.html { render action: 'edit' }
format.json { render json: @chore.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def chore_params
params.require(:chore).permit(:category_id, :title, :description, :price, :offers_accepted, :image_url, :location)
end
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iwGAo/8hp+6wvdCxTFNfMApFAfZRmxo946ID7t37M+Y=", "chore"=>{"category_id"=>"1", "title"=>"some text woohoo", "image_url"=>"http://placekitten.com/100/100", "description"=>"some more text woohoo", "price"=>"35", "location"=>"90210", "offers_accepted"=>"0"}, "commit"=>"Update Chore", "id"=>"5"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment