Rails can placeholder text be submitted? - ruby-on-rails

Can't find an answer anywhere in Docs... is there a way to make it so that the placeholder value for a number_field_tag or whatever is the value that is submitted to the value if the user doesn't enter anything else?
I.e., in my code below:
<%= number_field_tag "transaction[][#{thing}]", :quantity, min: 0, placeholder: #transactionparams ? #transactionparams["#{thing}"] : 0 %>
I would like the equation in placeholder to evaluate and then be POSTed if the user doesn't enter anything else.
Thanks!

HTML
The issue is HTML, not Rails
The problem is a placeholder is only there to give text that's visible only when the input has no value:
The placeholder attribute specifies a short hint that describes the
expected value of an input field (e.g. a sample value or a short
description of the expected format).
The short hint is displayed in the input field before the user enters
a value.
This attribute (placeholder) does not store any data in the input, meaning when you submit the form, it's not going to send anything to your backend. To fix this, you should should switch from using the placeholder to value attribute:
<%= number_field_tag "transaction[][#{thing}]", :quantity, min: 0, value: #transactionparams ? #transactionparams["#{thing}"] : 0 %>

No.
They will also automatically exclude the placeholder from being sent when the form is submitted.
You'll have to write some JavaScript to set the input's value property using its placeholder attribute, if its value is empty when the form is submitted.

Related

Ruby On Rails f.checkbox hidden field

I have a check box on a rails form like this:
.columns.small-1
= f.label :private
= f.check_box :private
if the box is unchecked, the form sends correctly with one value passed. If the box is checked the form sends the same field twice with one value as 0 and one as 1, which is causing the box to always show as unchecked" and have a value of 0. Been researching hidden fields in rails but not sure how to apply to this problem.
This is a normal behavior. By default, f.check_box renders a hidden field with value 0. Right after that hidden field, it renders an input field of type "checkbox" with the value 1. The input field and hidden fields have the same name. When both of these values are sent to the server, the later value (checkbox) will overwrite a param value with that name. This should be done automatically for you, unless you are trying to parse the params manually.

Rails 4 - Ransack Form - Format text field

My ransack form has a date to and from search using a datepicker for each. The search and results works great, however I would like to style the form fields so that when the results load, the datetime can be formated just to_date. I am unsure how to access the value used in the form. I could just use the value parameter and apply the styling, but I do not know the variable to use.
<%= f.text_field :order_date_lt, class: "", id: "datepicker1", value: ?????.to_date.strftime('%m-%d-%Y') %>
I found out that in this case #q.order_date_lt, if it is present, will be able to be formatted that way

Decimal button in RoR

I just managed to do a decimal migration.
But all of the sudden a weird button appeared next to the field.
Any idea how i can remove it or maybe the name of that weird thing?
Its on the right side of the field. And it has arrow up and down.
(I´m very new to coding)
This is the code
<%= f.input :phone, as: :decimal, placeholder: "Phone Nr.", label: "Contact", input_html: { rows: "1"} %>
Sorry, not allowed to post images yet.
It's a numeric input from HTML5, you may see this for description.
You may control input type by setting as parameter, possible values.
For most fields you may omit it, formtastic will guess correct input type by attribute type or name.
I think it would expect the phone attribute to be of a string type. If you need some why to keep it numeric but normal phone input is required, force it with as: :phone. But then you should have some preprocessing for this value before writing it to attribute.
The weird button is a part of number input. Read here about most common input types available in HTML5. It's purpose is to enter a decimal number. HTML5 introduced new input semantics so instead of boring box your browser will render additional controllers on top of the input depending on it's type.
What you probably want is a tel input. Rails provide <%= telephone_field() %>. Here is the list of all form helpers available in Rails.
Also consider using string for your phone number and validate it on model instead

Rails hidden_field_tag integer auto converts to string?

I have a form using a hidden_field_tag. The value I put as a parameter in the hidden_field_tag is an integer but when it comes out on the other side it's a string. Does hidden field tag auto convert this? Thanks
Short answer = yes
The real answer is it creates an input tag on the html side, the value you passed in as an integer gets set as value="your value here".. So anything in an input field will be a string.

in rails, What is the value returned in for a checkbox?

I want to set a cookie to expire in 1 year if the 'remember me' checkbox is checked.
I have a checkbox form input like:
<%= check_box_tag 'remember', '', false, :class => 'checkbox' %>
What will the value be when it gets posted?
Will it be true/false or checked or 1 or? I set the value to '' in the helper.
Checkbox will return either 0 or 1.
Don't compare with false and true, as this may cause you problems, since 0 is true in Ruby.
In the rails API documentation it has this helpful explanation of missing parameters and the hidden field work-around:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box
check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
Returns a checkbox tag tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). This object must be an instance object (#object) and not a local object. It’s intended that method returns an integer and if that integer is above zero, then the checkbox is checked. Additional options on the input tag can be passed as a hash with options. The checked_value defaults to 1 while the default unchecked_value is set to 0 which is convenient for boolean values.
Gotcha
The HTML specification says unchecked check boxes are not successful, and thus web browsers do not send them. Unfortunately this introduces a gotcha: if an Invoice model has a paid flag, and in the form that edits a paid invoice the user unchecks its check box, no paid parameter is sent. So, any mass-assignment idiom like
#invoice.update_attributes(params[:invoice])
wouldn’t update the flag.
To prevent this the helper generates an auxiliary hidden field before the very check box. The hidden field has the same name and its attributes mimic an unchecked check box.
This way, the client either sends only the hidden field (representing the check box is unchecked), or both fields. Since the HTML specification says key/value pairs have to be sent in the same order they appear in the form, and parameters extraction gets the last occurrence of any repeated key in the query string, that works for ordinary forms.
You get in your action the params
params['remember']
If if check you have the params with false like value. If it's not check you have no params send to your action.

Resources