Rails 3 basic validation not working with :prompt or :include_blank? -
i have next code in view:
<%= f.select :user_id, user_all_select_options, :include_blank => '--select name-----' %>
it displays list of users --select name-----
@ top. when user doesn't select name list , leaves --select name-----
selected errors because user_id blank.
for reference helper method code looks this:
def user_all_select_options user.all.map{ |user| [user.name, user.id] } end
my model follows:
class parcel < activerecord::base attr_accessible :parcel, :received_at, :received_by, :chilled, :courier, :location, :passed_to, :user_id, :user, :content, :localip validates :user_id, :presence => true belongs_to :user end
for reason validation doesn't appear running, if select user drop downwards , add together validation other field input application correctly shows user message stating field incorrectly empty.
interestingly if leave select drop downwards --select name-----
, maintain additional validation, exception thrown. doesn't prompt missing fields errors.
here record during error (this record when had validates presence check on location field:
{"utf8"=>"✓", "authenticity_token"=>"wm4kptoswp3xdv8uu4uasdadnszi9yfzmk=", "parcel"=>{"user_id"=>"", "received_by"=>"dan", "content"=>"", "chilled"=>"0", "courier"=>"", "location"=>"", "passed_to"=>"", "received_at(3i)"=>"9", "received_at(2i)"=>"2", "received_at(1i)"=>"2013", "received_at(4i)"=>"22", "received_at(5i)"=>"59"}, "commit"=>"receive parcel", "action"=>"create", "controller"=>"parcels"}
where should start looking? errors show when controller unless check against user.
the parcel controller create method looks this:
def create @parcel = parcel.new(params[:parcel]) @parcel.localip = request.env['remote_addr'] @parcel.received_by = @parcel.received_by.upcase unless @parcel.user.mobilenumber.blank? usermailer.parcel_notification(@parcel).deliver end respond_to |format| if @parcel.save format.html { redirect_to @parcel, notice: 'parcel created.' } format.json { render json: @parcel, status: :created, location: @parcel } else format.html { render action: "new" } format.json { render json: @parcel.errors, status: :unprocessable_entity } end end end
the reason why you're getting exception when don't select user line
unless @parcel.user.mobilenumber.blank?
since user_id
not set, @parcel.user
nil
causes exception. suggest move within @parcel.save
block.
def create @parcel = parcel.new(params[:parcel]) @parcel.localip = request.env['remote_addr'] @parcel.received_by = @parcel.received_by.upcase respond_to |format| if @parcel.save unless @parcel.user.mobilenumber.blank? usermailer.parcel_notification(@parcel).deliver end format.html { redirect_to @parcel, notice: 'parcel created.' } format.json { render json: @parcel, status: :created, location: @parcel } else format.html { render action: "new" } format.json { render json: @parcel.errors, status: :unprocessable_entity } end end end
ruby-on-rails-3 validation model controller
No comments:
Post a Comment