# Help in this code

**URL:** <https://www.ruby-forum.com/t/help-in-this-code/151432>\
**Category:** Rails\
**Created:** [October 21, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/help-in-this-code/151432 "2008-10-21T12:13:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 21, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/help-in-this-code/151432/1 "2008-10-21T12:13:35Z")

</div>

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

Any suggestions… thanks…

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 21, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/help-in-this-code/151432/2 "2008-10-21T20:10:36Z")

</div>

> 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  
> def add\_to\_cart  
> @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)

msg = ‘’

# stick all that logic in the cart model

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?

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 22, 2008, 12:26am UTC](https://www.ruby-forum.com/t/help-in-this-code/151432/3 "2008-10-22T00:26:28Z")

</div>

Ar Chron wrote:

> def add\_to\_cart  
> @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)
> 
> msg = ‘’
> 
> # stick all that logic in the cart model
> 
> 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…

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 22, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/help-in-this-code/151432/4 "2008-10-22T15:15:12Z")

</div>

Jay P. wrote:

> 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…
