Disabled Haml field is not passed to my rb file - ruby-on-rails

I have a single form which has some input fields. This form, firstly get values from database and fill them in. If they have value they will be disabled.
=text_field_tag("dt|#{a_key}",(begin;a_mock["dt_open"];rescue;nil;end), :class => "form-control", :disabled => a_mock["active"] == "true" )
In my rb file there is a rule which validate date. If the field is blank:
if !dt_open.blank?
...
end
What I noticed is if the field are disabled, it doesn't send. I know because I print the message and the field aren't there.
How can overcome this behavior in haml?

Input with disabled attribute will not submitted, you can user readonly rather disabled, did the almost same thing you can not change or edit input field value.
Another solution
If you still want to use the disabled attribute the you can apply the following javascript to allow submit the disabled
<script>
$('#your_form_id').submit(function() {
$("#disabled_input_field_id").prop('disabled', false);
//your code goes here
})
</script>

You can use a hidden_field_tag that will post the value on submit. I would also just print the value in a p tag instead of showing a disabled text input, but that is just my opinion.
- if a_mock["active"] == "true"
= hidden_field_tag "dt|#{a_key}", a_mock["dt_open"]
p= a_mock["dt_open"]
- else
= text_field_tag "dt|#{a_key}", a_mock["dt_open"], class: "form-control"

Related

Rails/ActiveAdmin form: how to disable an input field based on the value of another (boolean) input field?

I have a model that has a boolean field and an array field that gets the values populated from another model (foreign key, not really relevant to my question).
In ActiveAdmin, I have a form like such:
form do |f|
f.semantic_errors
f.inputs do
f.input :boolean_field_name
f.input :array_field_name,
as: :searchable_select,
ajax: true,
input_html: { disabled: true }
end
f.actions
end
The disabled: true works, but I would like to replace the true with something that evaluates whether or not the input checkbox for boolean_field_name has been checked on the form (which by default it isn't).
I've tried params[:boolean_field_name], params.key?[:boolean_field_name], f.object.boolean_field_name, f.object[:boolean_field_name], resource[:boolean_field_name] and resource.boolean_field_name, but they all do nothing and evaluate to nil as far as I can tell.
I've even tried ModelName.find(params[:id]).boolean_field_name but of course since params[:id] is nil that doesn't work, and it wouldn't find a record with that id anyway because the record hasn't been created yet.
I've tried looking through the ActiveAdmin repository but I can't find the information I'm looking for in the source code either.
Is this even possible?
Did you mean like this?
form do |f|
f.semantic_errors
f.inputs do
f.input :boolean_field_name
f.input :array_field_name,
as: :searchable_select,
ajax: true,
input_html: { disabled: f.object.boolean_field_name # <= does not work }
end
f.actions
end
Since the information is a bit ambiguous to me, I would firstly like to know:
From the description:
The disabled: true works, but I would like to replace the true with
something that evaluates whether or not the input checkbox for
boolean_field_name has been checked on the form (which by default it
isn't).
Did you mean that you wanna change the disabled attribute depending on the other form field after the page loads? If that's true, then you have to do it with javascript as something like this:
let booleanField = document.querySelector('booleanField'),
arrayField = document.querySelector('arrayField');
booleanField.addEventListener('change', function(e) {
arrayField.setAttribute('disabled', booleanField.value);
})
Or if you mean you just wanna set the value of the disabled attribute to what boolean_field initially is on page loads, it will bring us more information if you can debug with the tools like debug, debugger or binding.pry. It will be helpful to checkout what f.object.boolean_field_name returns. According to the information you provides, I guess it could really be nil.

Submit generated value to input Rails

I am creating a form in which an input in that form auto-generates a unique group_id. I set the value of that input to the randomly-generated string and set the input field to disabled so they can not edit it unless they click a check-box.
When I submit the form however the value does not go into the database even though I have the :group_id set in the permit params.
<%= form.text_field :group_id, id: :group_id, class: 'form-control inline', value: "DSC-" + "#{#pdnum.id}".rjust(3, '0').to_s, disabled: true %>
Does the value not get submitted into the database? If not, how can I get around this so that this randomly generated string is placed into the database?
Try to use readonly: true instead of disabled

How to prevent input from sending params

I have two inputs, both use the same field in the database. The difference between them is simple, one is just text input while the other makes it possible for the user to pick something from dropdown list. Might look like this:
= f.input :age, :label => false, input_html: {class: "textfield"}
= f.input :age, :label => false, input_html: {class: "dropdown"}, collection: [1,2,3]
in this case, only the second input will send information.
My question is: How can I prevent input from sending anything WITHOUT removing it from HTML?
(my purpose is to enable user pick option "custom age" which will enable them to fill the field with, well, custom value. tricky part is both input must be there all the time)
If you disable an input, its value is not sent when you submit the form. So you could use an onSubmit event on the form to disable the right input before the form is submitted.
With jQuery, it would look like this:
$('form').on('submit', function() {
var textInput = ...;
var dropdownInput = ...;
if (textInput.val() == '') {
textInput.prop('disabled', true);
} else {
dropdownInput .prop('disabled', true);
}
});

rails form - 2 fields with same id in a form - how to disable second field when first field is selected

I have in my rails form the following code:
<label>Fruit: </label> <%= f.select(:IST_FRUIT, [['Apple', 'Apple'],
['Orange', 'Orange'],
['Kiwi', 'Kiwi'],
['Other', 'Other']
],{ :prompt => "Please select"},{:onchange => "if (this.value == 'Other')
{document.getElementById('otherTissue').style.display = 'block';
} "}
) %>
<span id="otherFruit" style="display:none;"> If other, please state: <%= f.text_field :IST_FRUIT, :size => 10 %></span>
User can select a fruit form the listbox but if user selects 'Other', a textfield is diaplayed allowing user to input his value.
The problem is that when user selects a fruit and save the form, the fruit field in blank in the table and the reason is that the form is saving the second field 'IST_FRUIT' found in the span with id 'IST_FRUIT'.
I would be really grateful if someone could show me a way how to disable the second field when 'other' is not selected and enable it when 'Other' is selected from the dropdown list.
Many many thanks for any suggestion provided.
First, be aware that rails posts are based on fields' name, not ids.
In your case, you should use a virtual attribute to store the potential other fruit value.
Your model could look like:
attr_accessor :other_fruit
before_save :check_fruits
def check_fruits
#if other_fruit is not nil, it means that you want to store it's value in you IST_FRUIT column
#BTW, why these capital letters?
IST_FRUIT = other_fruit unless other_fruit.nil?
end
And your form would be:
<span id="otherFruit" style="display:none;"> If other, please state: <%= f.text_field :other_fruit, :size => 10 %></span>
I have another advice. Instead of disabling a HTML input (by the way - an ID must be unique in a HTML document) just make another attribute in your model, name it 'other_fruit' for example, and use this value if the 'fruit' is set to 'other' or is empty. For example you may write something like this:
class TheModel
attr :other_fruit
# Overwritten accessor for the fruit attribute.
# Returns the values of :other_fruit if :fruit is blank.
#
def fruit
if self[:fruit].blank?
other_fruit
else
self[:fruit]
end
end
end
Now the HTML part. In your JavaScript you set 'display' to block, but you do not reset it when user selects another option.
If you want to prevent the field from being sent to the server, you should set the 'disabled' attribute. Setting the 'style' only hides the control from the user's eyes.

How do i tell the check_box method to NOT add a hidden check_box for the 'unchecked' value?

I'm using rails 2.3.4, which, when you call .check_box on the 'f' object in a form_for, makes a visible checkbox input for the 'checked' value, and a hidden checkbox input for the 'unchecked' value: http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002434&name=check_box
The problem with this is that i have a validates_acceptance_of validation on the check_box, and if it's not checked, i'm getting a field_with_errors div wrapped around the visible checkbox AND the hidden checkbox, so that the error message appears twice.
In this instance i don't want a value passed through in the 'unchecked' case, so i don't want rails to add the hidden checkbox - this (switching off the hidden checkbox) would solve my problem. I can't figure out how to tell it to not add the hidden checkbox though. Can anyone tell me?
I know that i could get round this by making a check_box_tag, which doesn't add the hidden 'unchecked' case checkbox, but then i don't get field_with_errors stuff wrapped around the checkbox if it's not checked. Dispensing with the hidden field seems like the cleanest solution.
Thanks - max
In Rails 4 you can use include_hidden: false
example f.check_box :some_field, include_hidden: false
Since Rails 3.2, the hidden field will not be shown if the unchecked_value argument evaluates to false.
Example: f.check_box :your_field, {}, checked_value, false
See the Rails source: 3.2,
4.1
use <%= check_box_tag "your_model[your_field]" %>
f.check_box always gives you a hidden field.
So this is interesting. check_box takes four arguments as the method definition shows:
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
Tags::CheckBox.new(object_name, method, self, checked_value, unchecked_value, options).render
end
The checked value and unchecked value must be the third and fourth argument. By setting an unchecked_value that evaluates to a truthy value, then the hidden input will appear.
My initial goal was to have a visible checkbox with a value of true, and a hidden checkbox with a value of false. So I tried doing this:
<%= f.check_box :share, { }, true, false %>
However, because I actually passed in a boolean value for unchecked value, it made the hidden field not appear at all! I tracked it down to this line of code in the Rails source:
def hidden_field_for_checkbox(options)
#unchecked_value ? tag("input", options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => #unchecked_value)) : "".html_safe
end
If #unchecked_value evaluates to false or nil, then no hidden input field would display. Now if you want to send true and false values rather than '1' or '0' using check_box helper, then you will need to wrap the booleans in strings:
<%= f.check_box :share, { }, 'true', 'false' %>
But if that's the case, you matters well just use the defaults of '1' and '0' for boolean values and let Rails handle it.
Rails stores booleans as TinyInt, at least for the MySQL database. Consequently, that '1' will get stored in the database as 1 and will be considered a true value.

Resources