Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
check values before sending to controller in rails
6 Posts
check values before sending to controller in rails

In this code i have checkboxes for each item that give user ability to delete multiple items at once, but if the user press the remove button without checking any item it gives an error :


1) can i check this before sending to controller and gives an alert to user or it should be done in controller ?

2) if i want to add another method for editing multiple items at once, is it possible to this in this form ? i mean this is possible to have different actions for one form ?

Everything you're asking about is, theoretically, possible with JavaScript. The first questions is the most straightforward, and the easiest to solve. You can provide an onSubmit method to the form as an attribute, and in that method check if the user has selected any checkboxes. If you return false from the onSubmit method the form will not submit; you can display an error on the page by creating an HTML element, or simply show an alert box.

I'm not sure what exactly you'd like to for your second question. It's possible to have multiple forms on a page, and you could have each for submit via Ajax for a rich multiple-item edit experience. It's also theoretically possible to modify the attributes of the form itself (such as the URL you're posting to), although you should have an extremely compelling argument for doing something like that before you walk that path.

You should definitely not next forms inside one another, but if you can structure your page so the deletion checkboxes are in one form, and you have separate forms for editing each item, that may do what you want. Take a look at the #remote_form_for, #remote_function, and #link_to_remote methods in Rails; these will probably do for you most of what you want to do. If you need more sophisticated functionality you'll need to write the JavaScript by hand.

Thanks for your reply, for the second question what i am looking is something similar to wordpress (if you checked it), for example in edit posts page you can check multiple items and the select an action from a drop down list, delete edit , ... thats what i want to know how can it built thnx again

I think I see what you mean. I can't say there's a definitively best way to do something like this, but it will almost certainly involve JavaScript. Upon selection of an option you could iterate over the selected items and generate AJAX requests for each of them, based on which option the user selected.

As with any validation, you should do it in the controller to ensure nothing bad happens.

You can also then validate in javascript to provide a nicer user experience (and save bandwidth) but remember that javascript can be turned off and bypassed so you can't trust that the data you have received has been validated.

Something like this might work

function validateFields() { var checked = 0; $('form_name').elements.each = function(e){


} if(checked > 0) $('form_name').submit(); }

6 Posts
Login to add your message