views:

82

answers:

1

I'm building a Q&A app on Rails with a lot of complex validations (e.g. a user can't vote on his/her own questions, or vote on the same question twice, etc). I've been using Ajax+JQuery to update things on the page if the request goes through, but want to flash helpful error messages if there are problems. While I have no problem with client-side validations like checking to see if a field is blank, the best I can do for something like voting on your own question is prevent any javascript from being executed in the Votes controller, so that the votes counter doesn't update. Like this:

if @vote.save
  respond_to do |format|
    format.html {redirect_to :back}
    format.js
  end
else
  respond_to do |format|
    flash[:error] = "Sorry, there was an error."
    format.html {redirect_to :back}
  end

end

StackOverflow gives me an error message if I try to vote up my own question, so I know it can be done!

Thanks

A: 

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)
Trip
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
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 <% -if blah blah blah %>. Just add the `<%` 's.
Trip
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
Ah. My above examples are no good?
Trip
Hm, so you'd recommend performing validations in my controller rather than the model?
kateray
Updated above. Sorry I must have been all crazy about controllers. Models would work too, though I would personally stray away from that since its heavier on the DB where you could have otherwise had them load all this before-hand.
Trip
Strange, it's just not working! I copied your example exactly, but it doesn't seem to be sending an Ajax request, just a regular HTML refresh...
kateray