Thanks guys for your suggestions. I have now moved all the validations
to the model class and ended up with couple of question again. I've done
like the following:
MODEL CLASS:
--------------------------------------------------------------------------------
class Cart < ActiveRecord::Base
validates_presence_of :campsite, :arrival_date, :nights, :parents,
:total
def validate
errors.add(:nights, "should be equal to or less than 2") if nights >
2
errors.add(:total, "should be greater than 0") if total < 0.01
end
end
--------------------------------------------------------------------------------
CONTROLLER:
--------------------------------------------------------------------------------
def add_to_cart
cart=find_cart
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=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 cart.save
@cart << cart.id
@cart_details = find_items_in_session
else
render :action => :online_booking
end
end
--------------------------------------------------------------------------------
VIEW:
--------------------------------------------------------------------------------
<div>
<%=error_messages_for 'cart'%>
<%form_tag :action=>:add_to_cart do%>
<div id="this part gets replaced by partial>
<%=select_tag "campsite", %w{---}%>
</div>
...........
<%end%>
</div>
--------------------------------------------------------------------------------
1)Now, when I give 3 or more for the number of nights, online_booking
should be rendered again with the message "should be equal to or less
than 2", isn't it? but I don't get any message just the online_booking
page is redisplayed.
2)In the validation I have to validate that the number of parents and
children together can't be more than 8 (children can be null so it's not
checked). How do I do this?
3)One field gets replaced by partial before the form is submitted. So,
if I submit the form before making that partial loaded, id for that
field is passed as params[:campsite] and params[:camp][:id] if the
partial is loaded. How can I check for this? In the controller I'm tried
to do:
--------------------------------------------------------------------------------
if !params[:camp][:id].nil?
camp_loc = params[:camp][:id]
else
camp_loc = params[:campsite]
end
--------------------------------------------------------------------------------
but failed and I think the reason is: params[:camp][:id] is no where to
be found if the partial is not loaded and if I try with
params[:campsite] its the same if the partial is loaded. So, how can I
solve it as it gives me error in the same line where the if clause is?
thanks.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---