How to change date format in rails? - ruby-on-rails

I am trying to change date format for date_field_tag, but i am not getting proper.
code i have used:
<%= date_field_tag 'dob' , '' , value: #search_params[:dob], placeholder: 'YYYY/MM/DD', autocomplete: 'off', class: 'form-control' %>
i need to display format and save yyyy/mm/dd

<%= date_field_tag 'dob', #search_params[:dob].to_date.strftime("%Y/%m/%d"), placeholder: 'YYYY/MM/DD' %>
Note that date_field_tag method signature helper's signature is date_field_tag(name, value = nil, options = {})

Related

"Undefined" placeholder on f.select in form

I have a form
<%= form_for #user, url: contact_path do |form| %>
<%= form.select(:email, User.all.map(&:email), {}, { class: 'my-form' }) %>
<% end %>
which works well but has placeholder "Undefined" in start position.
I tried to get rid of that with
<%= form.select(:email, User.all.map(&:email), {placeholder: "Select email"}, { class: 'my-form' }) %>
or
<%= form.select(:email, User.all.map(&:email), {prompt: "Select email"}, { class: 'my-form' }) %>
but still same. Any ideas?
<%= form.select :email, options_for_select(User.all.map(&:email)), include_blank: "whatever your prompt says" , class: 'my-form' %>
Experienced same issues today and this is what I did for one of our projects recently.
<%= f.select :category, options_for_select(Category.all.collect { |c| [c.name, c.id] }), { include_blank: 'Select category' }, { class: 'custom-select' } %>
Try changing the values according to your values.
This is how you add class and placeholder in a form.select element in Rails.

Rails form datepicker

In a checkout I'm prompting a user to confirm his address and required_date to receive the part. The form is shown for the #auction attributes that are needed to be confirmed. All of the address lines are already populated. But the date field is cleared, I want the date field to be populated with the date that was already chosen(#auction.required_date), so that the user doesn't have to re-pick the date if the required_date has not changed.
Here's the form
.form-container
%p#alert= alert
= form_for(#auction, html: { class: "inline-form" }, remote: true ) do |f|
- if #auction.errors.any?
#error_explanation
%h2
= pluralize(#auction.errors.count, "error")
prohibited this auction from being saved:
%ul
- #auction.errors.full_messages.each do |message|
%li= message
.field.form-group
= f.text_field :destination_address, class: "form-control", placeholder: "Destination Street Address"
.field.form-group
= f.text_field :destination_city, class: "form-control", placeholder: "Destination City"
.field.form-group
= f.text_field :destination_state, class: "form-control", placeholder: "Destination State/Region"
.field.form-group
= f.text_field :destination_zip, class: "form-control", placeholder: "Destination Zip Code"
.field.form-group
= f.text_field :destination_country, class: "form-control", placeholder: "Destination Country"
.field
= f.date_field :required_date, class: "form-control datepicker", placeholder: "Required Delivery Date"
%br/
.actions.submit_button
= f.submit 'Submit', class: "btn custom-btn"
%br/
:javascript
$('.datepicker').pickadate({
selectMonths: true
});
I believe you can just do this:
= f.date_field :required_date, class: "form-control datepicker", placeholder: "Required Delivery Date", value: #auction.required_date
and it should populate the date field.
I suppose you have to add value in right format using strftime, for example:
value: #auction.required_date.strftime('%m/%d/%Y')

Rails html.erb Select input convert to Slim

I am trying to convert all my html.erb files into .slim in my new project. I am missing last one inside of my form - a select input.
The form.html.erb looks
<%= form_for #user do |f| %>
<%= f.hidden_field :document_id, value: #doc.id %>
<%= f.label :Obor %>
<%= select(:user, :job, [
["Choose work"],
["Admin"],
["Other"]
], {disabled: "Choose work", selected: "Choose work"}, class: "form") %>
<%= f.button "Submit" %>
<% end%>
Everything works just in the way I want. I tried to make it in a .slim in this way:
= form_for #user do |f|
= f.hidden_field :document_id, value: #doc.id
= f.label :Obor
= select(:user, :job, [
["Choose work"],
["Admin"],
["Other"]
], {disabled: "Choose work", selected: "Choose work"}, class: "form")
= f.button "Submit"
It does not work with an error: Unknown line indicator ->
new.slim, Line xx, Column xx
["Choose work"]
How I can convert the select from html.erb into .slim? Thank you.
EDIT - ANSWER: Thanks to erb_to_slim gem that Ricardo mentioned I figured out the solution. I simply missed the rails indicator on each row of the selector. The answer is:
= form_for #user do |f|
= f.hidden_field :document_id, value: #doc.id
= f.label :Obor
= select(:user, :job, [
= ["Choose work"],
= ["Admin"],
= ["Other"]
= ], {disabled: "Choose work", selected: "Choose work"}, class: "form")
= f.button "Submit"
Do you tryed to use the gem erb_to_slim?
In your terminal type:
gem install erb_to_slim
And inside your project folder, use:
erb_to_slim
All erb files will be converted to slim and the erb files will be renamed.
Maybe this can help.

Change options_for_select to radio buttons

Hi i'm trying to change the following:
.form-group
= label_tag :metric, 'Metric', class: 'sr-only'
= select_tag :metric, options_for_select(Report::METRICS, selected: #report.metric), class: 'form-control'
to something like the following:
.form-group.pull-left
= label_tag :metric, 'Metric', class: 'sr-only'
.btn-group{"data-toggle" => "buttons"}
%label.btn.btn-default.active
%input#option1{name: "options", type: "radio", value: "value1"}/
Value1
%label.btn.btn-default
%input#option2{name: "options", type: "radio", value: "value2"}/
Value2
However the problem is that when I submit the form, it doesn't actually submit the radio button value. Also after it submits, it defaults back to no selection when it should remain on the selected radio button.
You can use the radio_button_tag helper and set the checked parameter correctly.
Using your example, it's something like this:
.form-group.pull-left
= label_tag :metric, 'Metric', class: 'sr-only'
.btn-group{"data-toggle" => "buttons"}
%label.btn.btn-default.active
= radio_button_tag('options', 'value1', params[:options] == 'value1')
Value1
%label.btn.btn-default
= radio_button_tag('options', 'value2', params[:options] == 'value2')
Value2

Rails - simple form

I use simple_form gem in my Rails app. When I use code like this
= f.input_field :name, label: false, placeholder: 'Name'
= f.input_field :price, label: false, placeholder: 'Price'
= f.input_field :description, label: false, placeholder: 'Description'
= f.input_field :image, label: false, placeholder: 'Image'
I get HTML for input:
<input class="string required textform" id="item_name" label="false" maxlength="255" name="item[name]" placeholder="Имя" size="255" type="text">
As you can see size of input is 255 now. Actually it's much more than enough. How can specify the size of inputs?
Following is from simple_form documentation here
It is also possible to pass any html attribute straight to the input,
by using the :input_html option, for instance:
<%= simple_form_for #user do |f| %>
<%= f.input :username, input_html: { class: 'special' } %>
<%= f.input :password, input_html: { maxlength: 20 } %>
<%= f.input :remember_me, input_html: { value: '1' } %>
<%= f.button :submit %>
<% end %>
To set size of 100 for name input:
= f.input :name, label: false, placeholder: 'Name', input_html: { size: 100 }

Resources