How do I perform complex, server-side form validation with JQuery/Ajax (and Rails)?

Server Side Validation In your Vote model : validates_uniqueness_of :current_user Keep your traditional controller set up for edit and save. And then with jquery use this : $(". Vote_link").

Submit(function(){ $. Ajax({type: "POST", url: $(this). Attr("action"), data: $(this).serialize(), dataType: "script", error: function(){ $("#message").show().

Html("You can't vote on this! ")}, success: function(){ $("#message").show(). Html("You voted!")}; }); return false; }); And your HTML/HAML: = link_to 'Vote on This', new_vote_path(object).

Yes, the validation above would work, but it would just reply false if the validation didn't pass (which is what my code already does), and not send an Ajax-y error message about why. The bottom code doesn't quite make sense to me. Is that meant to be in a javascript file?

The . Js files don't have access to variables like @question, right? – kateray Aug 23 '10 at 21:56 Are you trying to stay away from Ruby all together?

I'll have to think about that ( using pure ajax to solve this.. ). But the bottom example is something that you can use via html/haml . Haml being shorthand for HTML.

Its syntax is a little diferent. It just doesn't have the . Just add the – Trip Aug 23 '10 at 22:09 I don't have any problem with Ruby, I've written validations already that do everything I want them to.

The problem is that I'm using Ajax to update various elements on the page (like the vote count, for instance) that shouldn't be updated if the request doesn't go through. There should be an error message if the request doesn't go through. The problem is that I need to send an HTML request to check those validations - which is why I wanted to find a way to do it through Ajax.

– kateray Aug 25 '10 at 14:56 Ah. My above examples are no good? – Trip Aug 25 '10 at 15:30 Hm, so you'd recommend performing validations in my controller rather than the model?

– kateray Aug 26 '10 at 1:21.

Keep your validations, generate HTML to be innerHTML'd into place with javascript sent back as a response, like so (with jQuery): votes/create.js. Erb: $('#vote_form'). Html(" 'form').

Html_safe) -%>"); $('#vote_form'). Html(" 'success'). Html_safe) -%>"); It'll save you some headache.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions