simple_form: required vs. optional fields via AJAX - ruby-on-rails

I have a form using simple_form, and trying to update some optional fields in it to required with AJAX.
In the Rails js.haml template, I am adding the class 'required' to the field label and input, e.g.:
$("label[for=some_label]").addClass('required')
$("input[id=some_input_id_here]").addClass('required')
but it does not work.
Has anyone done a similar thing with AJAX and simple form?
Another option is to re-render the form from the js.haml template, and to set :required => true for the necessary fields, however I think this is an overkill.
Ideas?
Thanks

Related

Using render as the value_method in Rails Simple Form?

I'm working on an RoR project with the Simple_Form gem. For those who don't know, simple form creates easier form helpers for use in views. My question is regarding the f.association helper which allows users to easily select associations on models via a select box, radio buttons or checkboxes.
f.association has an option called value_method which allows you to pass a method for generating the name of the individual models in the association to choose from. The default is just to_s, so it will work with select boxes. However, I'm using radio buttons, and I'd like to know if anyone knows of a way to use the render partial method as the value_method.
In my setup, I have a User model with a user.haml partial that I'd like to be rendered next to each checkbox, that way users selecting other users can have any easy to recognize user div with picture, name, and last login to choose.
Thanks, steakchaser answered my question with his comment. This is the solution:
f.association :some_association, label_method: lambda { |obj| render obj }
I realized label method is better than value method, because even with checkboxes and radio buttons, simple form still put the values in the options value="..." attribute. This caused an html rendering error.

How to add a hidden field to all simple_form generated forms?

How to add a hidden field tag to all simple_form generated forms in Rails 3.2?
Or how can I add a hidden input field to all forms (both form_for and form_tag generated forms)?
I'm asking to do it in a generic way, I have hundreds of such forms and I want to add in all of them, not just single form.
I am expecting something involving form builders, I tried them but I am not getting it right.

How to disable all elements on form with rails 3?

I have a partial which includes a form, i have two different instances where i want to use this form, one if for editing and another one if for viewing only, i want to disable all of the fields in a second case. Is there any quick way to do it in rails? I know i can do it with jquery but i prefer to do it in rails.
You can wrap all elements in with_options :disabled => 'disabled' do method.

Extra item in a rails form

I want to create an extra radio button with some choices inside an existing rails form (I use simple_form gem). The problem is that I do not want this to point in any field in my model. I want it to be an extra field that I would like to pass.
All examples that I found introduces a hidden tag. But I do not want it to be hidden I want a radio button set.
Is that possible?
Try using radio_button_tag inside the modal form, I have used it in the past. An example would be:
radio_button_tag 'send_me_updates', 'radio', false
When you submit the form, I think you will be able to access the radio button value with params[:send_me_updates]. Should work, please try it and let me know.

How to create one form with many possible actions in Rails?

I want to create one form with 2 buttons in rails. Both forms operate on the same data in different ways, and I would prefer to keep the associated functionality in two different methods. Previously I've redirected to the appropriate method after doing a string comparision on params[:commit], but my gut says there's a better approach. Suggestions?
Two different submit buttons that send the form to two different actions:
<%= submit_tag('Insert', :onclick=>"document.myForm.action = 'insert';") %>
<%= submit_tag('Update', :onclick=>"document.myForm.action = 'update';") %>
Instead of "myForm" you need to put whatever is in the "name" property of your tag.
You can set that property in your default form:for tag like this:
<%= form_for(#something, :html => {:name => "myForm"}) do |f| %>
Without using JavaScript, your only solution is what you mention: checking which button was clicked by looking at the POST data in the controller. This is simply due to the nature of the HTML form element. It cannot have more than one value for its action attribute.
If you're not worried about what will happen when JavaScript isn't available, then you can write some script to change the action attribute when one of the submit buttons is clicked, prior to actually submitting the form. In the case of an ajax request, it could simply submit to the correct URL directly without altering attributes on the form.
I also used the params[:commit] method on a form already. Using the I18n helpers makes this a bit less fragile as you can use the same lookup in the view and controller, so you don't encounter the problem that the string changes a bit.
Besides that I can only think of using JavaScript to handle the clicks on the buttons and then send the form data to different Rails actions (Maybe you can change the HTML action attribute of the form with JavaScript before you submit the form).
If you're using prototype.js, you can use Form.serialize() to grab your data from your form and from there use the different buttons to post to different actions.

Resources