I have an action like this:
def add_credit_card
if request.post?
unless params[:conditions]
flash[:error] = 'You need to accept!'
end
end
end
This action renders the following view:
<%= form_tag do %>
<fieldset>
<%= check_box_tag "conditions"%> I agree to the <%= link_to "Terms and Conditions", consumer_terms_and_conditions_url, :target => "_blank" %>
</fieldset>
<%= submit_tag "Submit" %>
<% end %>
When I do a GET to that action no errors are shown. When I do a submit with that box checked no errors are shown. When I do the first submit without that box checked the error is shown, but the problem comes when I do another submit and the checkbox is not checked, the errors are still there.
My questions are:
- Why is that happening?
- What would be a better approach to deal with this situation, where a form is not attached to a model and the errors have to be shown just when the user has submitted the form?
Since you’re using the same action, you’ll want to use flash.now so the flash hash does not persist to the next action.
Also, it’s not essential, but consider using:
and then checking the value of the params[:conditions] for the string “accepted” i.e.: