Hi, I’m trying to validate couple of things in the controller as I
couldn’t figure out how I can implement the same in the models. I’m
trying to check the ‘children’ field in the form and if its nil, it
checks the ‘family claim’ check box and if its ticked show error message
and if this is validated ok then checks that the sum of children and
parents field’s values don’t exceed 8. If everything’s fine then do the
saving in the carts table. Here’s how I’ve implemented it but doesn’t do
the job for the combination of children and family claim field:
CONTROLLER:
def add_to_cart
if params[:children].nil?
if params[:family_claim]==“1”
return (redirect_to_booking(“You can’t claim for family rate
without children.”))
end
else
if ((params[:parents].to_i) + (params[:children].to_i)) > 8
return (redirect_to_booking(“Maximum 8 people in a group.”))
end
end
if params[:family_claim] != “1”
total = (((params[:parents].to_i + params[:children].to_i) * 4.85)
params[:nights].to_i)
else
total = (19.4 * params[:nights].to_i)
end
@cart=find_cart
carts=Cart.create(:campsite => params[:camp][:id], :arrival_date =>
params[:arrival_date],
:nights => params[:nights], :parents =>
params[:parents],
:children => params[:children], :family_claim =>
params[:family_claim],
:total => total)
if carts.save @cart << carts.id @cart_details = find_items_in_session
else
redirect_to_booking(“Invalid input. Details couldn’t be stored.
Please Try Again…”)
end
end
def add_to_cart
if params[:children].nil?
if params[:family_claim]==“1”
return (redirect_to_booking(“You can’t claim for family rate
without children.”))
end
else
if ((params[:parents].to_i) + (params[:children].to_i)) > 8
return (redirect_to_booking(“Maximum 8 people in a group.”))
end
end
if params[:family_claim] != “1”
total = (((params[:parents].to_i + params[:children].to_i) * 4.85)
if carts.validate_input_rules(msg)
if carts.save @cart << carts.id @cart_details = find_items_in_session
else
msg = ‘Bad input, try again msg’
end
end
unless msg.empty?
redirect_to_booking msg
end
end
if carts.validate_input_rules(msg)
if carts.save @cart << carts.id @cart_details = find_items_in_session
else
msg = ‘Bad input, try again msg’
end
end
unless msg.empty?
redirect_to_booking msg
end
end
Maybe?
thanks for the reply Ar Chron!! but what will the implementation of the
‘validate_input_rules()’ in model be like coz the code i presented in my
first post don’t seem to do what it is meant for as even though the
children field is empty and user select the family rate claim check box
it gets validated ok and the details get saved which actually shouldn’t
happen. so, how can i improve tat code to eliminate this bug? thanks…
even though the
children field is empty and user select the family rate claim check box
it gets validated ok and the details get saved which actually shouldn’t
happen. so, how can i improve tat code to eliminate this bug? thanks…
See if you’re receiving an empty parameter in params[:children]…
Raqther than params[:children].nil?, try .empty?
def validate_input_rules
msg = ‘’
if self.children.empty?
if self.family_claim == ‘1’
msg = No family rate without children.’
end
else
if self.parents.to_i + self.children.to_i > 8
msg = ‘Maximum 8 people in a group.’
end
end
if self.family_claim != ‘1’
total = (((self.parents.to_i + self.children.to_i) * 4.85)
self.nights.to_i)
else
total = (19.4 * self.nights.to_i)
end
return msg
end
I’ve been bitten by the nil vs empty enough times in the past…
and check that code to make sure I didn’t fubar something…
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.