Model validation being bypassed - ruby-on-rails

I have a Comment model, in which I validate the presence of all fields. However, I used AJAX to submit and display a comment. Now, the validation is no more working and users can submit empty comments. How do I enforce validation?

If you want to use active record validation with AJAX, you need to implement it through rjs.
Have a look at these examples
http://railscasts.com/episodes/43-ajax-with-rjs
http://minimalbugs.com/questions/how-to-make-ajax-with-rjs
http://www.rubyinside.com/16-rjs-resources-and-tutorials-for-rails-programmers-5.html

Related

Devise invitable and validating nested attributes

I have an app where users can invite other users, and I have added a has_many model Enrollment model that has several attributes.
The devise_invitable module seems to ignore validations, so in my case, if my nested attributes fail validation, it is never caught, and the user still gets invited.
I found the validate_on_invite (defaults to false) setting, which may work. But then, I would need to add conditional validations to the other User attributes (name, address, etc.) so that they do not trigger during the invitation process.
I have a custom create controller method right now, and can add some logic to check the presence of parameters, etc. However, is there an easier way to do this that relies on the model validations?
UPDATE
I tried changing validate_on_invite to true and got nested attributes and validations working. The problem I have now is that in the User model you can't (as far as I can tell) do a conditional validation based on the controller action (i.e. the invitations controller with the create action).
I think I am back to some custom code in the invitations controller.
I ended up stumbling upon if: :accepted_or_not_invited?. I added this to all the User validations (other than email address). I can invite users with nested enrollment attributes that are automatically saved. I don't have the nested validations working yet but for not I added a monkey patch in my invitations controller to validate the enrollment params.

how to apply validation if I using wicked gem

I use wicked gem for devide page in multistep.
But there is one model.
How can i fire validation on this two different page
Is there any solution please help me...
There is two controller first is book_controller from that it enter in multistep controller from this controller it update book params.
and there is one model book_model form where i write validation
but in first page it fire all validation.
actually i want to fire few validation on first page and remaining validation in steps from
You can't do it on server side.
So, the solution do it on the client side.
http://jqueryvalidation.org/documentation/
http://railscasts.com/episodes/263-client-side-validations

Captcha with Multipart Form Rails

I have a multipart form that I need a captcha for at the end. Essentially, a user is allowed to create/update a draft but not submit it for admin review until everything is done. There is a captcha meant for the last submission but the problem is that when I add it to the form, I can't use any of the other submit buttons because the captcha isn't filled out. Is there any way around this?
I'm using simple_captcha and Rails 3.2.
Thanks!
I haven't used simple_captcha before, but seems like you are doing #object.save_with_captcha in every case. You have multiple options to solve this, but one i came up with is:
In the controller, verify if all the fields (mandatory only i guess) are filled, and if they are, then save your object using #object.save_with_captcha, otherwise do the usual #object.save which wont trigger the captcha validation. Something like this:
def create
#object = MyObject.new(params[:my_object])
if #object.has_mandatory_fields_filled?
#object.save_with_captcha
else
#object.save
end
end
In the has_mandatory_fields_filled? method you would check that all the mandatory fields of your form are not empty/nil etc.

Ruby on rails form validation for user profiles advice needed

I have one model that holds validation rules for my edit_profiles page. On the edit profile page I'm using jquery accordion to split user edit_profile into different sections for users to edit information. Each section is a separate form.
e.g.
Basic info (form 1)
Personal Stats (form 2)
Favourite things (form 3)
About me (form 4)
My problem is successfully filling out information on one form and clicking update is unsuccessful because other validation rules that have been set are firing in because other forms are failing validation because they have not yet be filled in.
I've tried to use the validation_group gem but this seems to have no affect.
I'd like to know if there is an easy way to do this?
Can't I just bunch up validation rules for each form and put them in separate methods and only make them come into play when the update button from a matching form has been clicked?
So if the update button on form 1 is clicked the form_one_validations method would be fire for example and the unrelated validation methods won't.
I would really really appreciate an example of how to do this.
This is the action responsible for y edit_profile view:
def edit_profile
#profile = Profile.find_by_user_id(current_user.id)
end
It is based inside my profiles controller.
Kind regards
I ended up using :allow_blank
This way the fields don't have to be filled in but all other important validation rules are still enforced if they need to be.
I can propose you such approach: you can add additional fields to your model, something like "basic_info_completed" which will be set up once after user has filled all corresponding information. And make all necessary validations conditional and perform them only when such field is set to true. So before user fills all fields of profile section, they are all can be edited without validation, but after profile fields are completed, validation is turned on for that part of profile.

Rails Form Validations for Multi-Model Forms

I am trying to build a Rails app with multiple models in a single form, and multiple forms on a single page. To make that work (according to my limited knowledge), I have to drop out of the scaffold code and the "form_for :model" helper and use "form_tag" instead. However when I do that, I lose the ability to automatically catch and report form validation errors in the view (with the error message in the flash[:error] and have the invalid fields highlighted.
If I have a controller method for a form that has to validate data from multiple models, how to I pass the validation errors back to the form? What do I have to do to get the invalid fields highlighted?
(For the longest time I didn't "get" Rails forms, because I thought they were useless Ruby wrappers for HTML code. Now that I am working in a non-Rails environment, I realize how much hard work they save because validation is tied to ActiveRecord validaions, and if a validation fails, the form can be reposted with the invalid fields hightlighted and a useful message in flash[:error]).
To add multiples models to a simple form, after rails 2.3 you just have to add accepts_nested_attributes_for in your model, the model that will be connected with your controllers and views, change the views to support information from another models (with field_for) and maybe build the reference objects in your controllers. Check these links:
http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes.
http://github.com/alloy/complex-form-examples

Resources