Who You Calling A Jesse?

Trying to sort the brilliant ideas from the lesser ones.

Validate check box data in rails

Posted by Jesse Rodgers on October 06, 2008 at 10:32 PM

A friend is working on a rails app for me that will run the voting for the UW Staff Association. We had some simple requirements and she threw together the app pretty quickly. The poll requires options like ‘select one of three’ or ‘select three of five’ so there had to be check boxes that have id values… look something like:

input id="option[ids][]" name="option[ids][]" value="36" type="checkbox" 

In the case of ‘select three of five,’ if you accept the value of the input you need to check someone didn’t just change the value of all the boxes to the same so they don’t vote three times for one thing. This was her quick fix:

  unless params[:option][:ids].uniq.size == params[:option][:ids].size
    flash.now[:error] = "You can't vote for the same thing multiple times in one poll?"
    return
  end

The check just makes sure the same id’s aren’t submitted twice. Being a total rails novice I expected rails would have some pretty way to do this. The above is good (it works) but I had expected a more elegant data validation… I must expect too much.

Hierarchy: previous, next

Comments

There are 4 comments on this post. Post yours →

’?’? I would imagine that error messages shouldn’t really be questions.

I personally am not that big a fan of Ruby. I tried to use it to replace a bunch of Perl tools that I wrote but a lot of the libraries I tried to use have annoying bugs. I haven’t even looked at Rails but slapping Prototype on Ruby and shoehorning it into an MVC framework just don’t appeal to me. Sorry Ruby folks.

I have also looked at Python (since Google uses it) and wasn’t too impressed with it either. The way it interacts with the web server is similar to Ruby and I find it frustrating when coming from PHP/ASP.Net. I like the flexibility of serving static and dynamic pages from the same tree without requiring crazy apache configs.

Correct me if I’m out to lunch here.

Doug S

Looks like a dodgy solution to me. I don’t know Ruby, but I bet that code treats the parameters like strings, and your future code treats it like integers. So:

value=”36”
value=”36.0”

Probably gets through.

Jesse Rodgers

@Adam – If you have Perl and like it… then I wouldn’t think Ruby is for you. It is a tool that you can love or hate…

@Doug – Indeed it is dodgy…

Hehe. I can officially say that all the Perl has been retired. (And I didn’t like it when I used it, just pushed there by IST).

Post a comment

Required fields in bold.