How to Disable All Form_For Input Fields in Ruby on Rails App

How to disable all form_for input fields in Ruby on Rails app?

Javascript

One way would be to do it using JS. Include a div with a specific class in the show view :

// show.html.erb

<div class='disable_input'>
<%= form_for(@project) do |f| %>
<%= render 'fields', :f => f %>
<% end %>
</div>

Then in your JS file :

$('.disable_input :input').prop('disabled', true);

Rails

If you want to actually generate it server side, you can pass a variable to your partial that will tell the partial if it has to add the disabled option on each field. It's a bit more work though!

Using a variable, you could do something like this :

<%= form_for(@project) do |f| %>
<%= render 'fields', :f => f, :disabled => true %>
<% end %>

In the partial :

<% disabled ||= false  
#We do this so if disabled is not passed to the partial it doesn't crash.
# We default it to false
%>

<% # Then for all your fields, add disabled: disabled %>
<%= f.text_field :some_attribute, disabled: disabled %>

Form builder

Edit : actually, one way to avoid explicitly passing disabled everywhere would be to create a Custom form builder. There's some good resources talking about it, like this one : http://johnford.is/writing-a-custom-formbuilder-in-rails/

In this example, it's done for onkeypress, shouldn't be hard to adapt for your case!

Ruby on Rails, disable a text field in a form only if action new

You can try this:

<%= f.label :email %> 
<%= f.email_field :email, class: 'form-control', disabled: !f.object.new_record?%>

How to disable temporarily a form using RJS templates

Have you looked at the :disable_with => 'Saving...' code for the submit button? This was created for exactly why you are trying to do. All this actually does is add the data attribute 'data-disable-with' to the submit button.

If you actually want to disable the whole form you could take the logic directly from your javascript driver in rails and change it to disable the whole form.

As an example the disable-with code for the jQuery driver is

    /**
* disable-with handlers
*/
var disable_with_input_selector = 'input[data-disable-with]';
var disable_with_form_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')';

$(disable_with_form_selector).live('ajax:before', function () {
$(this).find(disable_with_input_selector).each(function () {
var input = $(this);
input.data('enable-with', input.val())
.attr('value', input.attr('data-disable-with'))
.attr('disabled', 'disabled');
});
});

$(disable_with_form_selector).live('ajax:complete', function () {
$(this).find(disable_with_input_selector).each(function () {
var input = $(this);
input.removeAttr('disabled')
.val(input.data('enable-with'));
});
});

How to disable specific value in dropdown select with simple_form?

Simpleform builder for select box uses value of the each option to compare with value of disabled attribute, so you just simple have to use id of the organization to disable desired option:

= simple_form_for(@organization,url: admin_organization_path) do |f| 
= f.input :hospital_name, input_html: { class: "form-control"}
= f.input :parent, collection: @organizations, input_html: { class: "form-control", id: "chosen-select-speciality"}, disabled: @organizations.first.id

If you want to disable several options for selectbox, you can manually build options list inline or using helper and use it as attribute for input:

= f.input :parent,  collection: @organizations.map{|o| [o.id, o.name, {disabled: o.id.in?([1,21,10])}]}, input_html: { class: "form-control", id: "chosen-select-speciality"}

How can I disable pre-fill form fields in Rails?

How to disable the pre-fill form in Rails?

Adding value: nil for the field you want to disable pre-fill will do.

For example,

<%= f.text_field :email, value: nil, placeholder: "New Email", class: "formfield" %>

What should be modified to accept blank emails in the settings form
without updating the email to blank in database?

Erase presence: true in the email validation.

validates :email, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }

How to disable a form submit button a là Ruby on Rails Way?

The Rails jQuery bridge (jquery_ujs.js) code actually has a helper for this.

<%= form.submit "Save", id: "button_id", class: "button", disable_with: "Submitting..."

It will replace the button text with the value you give.

See http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-submit_tag

API change: as Renan suggests in a comment below, as of Rails 4 beta there is a deprecation notice on the disable_with option. It should be changed to be a data attribute:

<%= form.submit "Save", id: "button_id", class: "button", data: {disable_with: "Submitting..."} %>

This should work with all recent versions of Rails as it's what the option did anyway. So it'll be one less deprecation notice to fix when you upgrade to Rails 4. :)



Related Topics



Leave a reply



Submit