I have the following text_field:
<%= f.text_field( :with_created_at_gte, class: 'form-control datepicker filterrific-periodically-observed', 'data-provide': 'datepicker', "data-date-clearBtn": true ) %>
Which outputs:
<input class="form-control datepicker filterrific-periodically-observed" data-provide="datepicker" data-date-clearbtn="true" type="text" value="05/08/2015" name="filterrific[with_created_at_gte]" id="filterrific_with_created_at_gte">
The problem at hand is: my attribute is data-date-clearBtn, but Rails renders data-date-clearbtn (lowercase b). How can I avoid this problem? I also tried:
<%= f.text_field( :with_created_at_gte, class: 'form-control datepicker filterrific-periodically-observed', data: { 'provide': 'datepicker', "date-clearBtn": true } ) %>
Consider that html tags and attributes are case insensitive, check the comments in this answer, so Rails is actually correct in this situation.
I don't think you can overwrite that behavior directly, you should either write your helper or type the html tag manually. The rails helper is designed in that way
Related
I am passing a hidden field for a form to distinguish between views the request came from:
<%= hidden_field_tag("advanced", true)%>
Apparently true gets passed as a string. I tried different syntaxes like:
<%= hidden_field_tag "advanced", true %>
<%= hidden_field_tag "advanced" => true %>
<%= hidden_field_tag :advanced => true %>
It always gets translated to this
<input type="hidden" name="advanced" id="advanced" value="true" />
Obviously, I can check the string value in the controller, but is this the expected behaviour?
Rails translate your parameters to the equivalent html, and it is only possible to have strings in html. The hidden_field_tag is just a hidden text_field_tag, so the value need to be text. And in the url you also have a string. So the conversion need to be done in the controller.
how can I style the date format of a date_field?
I got the following form:
<%= form_for([#user, #vehicle]) do |f| %>
<%= f.label :name, "Vehicle name" %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :matriculation_date %>
<%= f.date_field :matriculation_date, class: 'form-control' %>
<%= f.submit "Add", class: "btn btn-primary" %>
<% end %>
this is rendering the following HTML for the date field:
<input class="form-control" type="date" name="vehicle[matriculation_date]" id="vehicle_matriculation_date">
The date field accept input in the following format: mm/dd/yyyy. I want it to accept format like dd/mm/yyyy.
I tried to edit it.yml file without any luck.
it:
date:
formats:
default: ! '%d/%m/%Y'
datetime:
formats:
default: ! '%d/%m/%Y %H:%M'
Any clue? Thank you
EDIT
None of the proposed solutions solve my problem, so I think my question was not very clear. What I want is just to style how the date_field prompt the user (see image for more details). At the moment the form show mm/dd/yyyy, and I want it to show dd/mm/yyyy.
Not sure it might be a useful information, but I'm using Bootstrap for styling.
You can do:
<%= f.date_field :matriculation_date, as: :date, value: f.object.try(:strftime,"%m/%d/%Y"), class: 'form-control' %>
You can convert the date formate with strftime()
#date.strftime("%d/%m/%Y")
It will convert your date into dd/mm/yyyy formate as you wanted.
Here I am showing you an example:
t = Time.now
=> 2016-04-28 16:09:42 -0700
>> t.strftime("%d/%m/%Y")
=> "28/04/2016"
for more strftime() formatting options you can check this http://apidock.com/ruby/DateTime/strftime
EDIT :
After reading your comment and screenshot i've got the solution as follow.
You can also use this gem: https://github.com/Nerian/bootstrap-datepicker-rails
on your gemfile.rb
gem 'bootstrap-datepicker-rails'
then bundle install and restart rails server
then add this line to app/assets/stylesheets/application.css
*= require bootstrap-datepicker
and add this line to app/assets/javascripts/application.js
//= require bootstrap-datepicker
and to use this in your form:
<%= f.date_field :matriculation_date, :id => "datepicker" %>
and add this javascript on the same view of your form
<script>
$('#datepicker').datepicker({format: 'dd/mm/yyyy'});
</script>
You can try this
#yourdate.strftime("%m/%d/%Y")
this works!
My answer is based on #Dharmesh_Rupani answer
Follow the steps to add the gem 'bootstrap-datepicker-rails'
I made two little changes on the form
<%= f.text_field : matriculation_date, :placeholder => 'dd/mm/yyyy', class: 'form-control datepicker' %>
The first thing is I changed the type of the field to text, because when I implemented I had two calendars, one from the date_field and another from the new gem
The other thing I changed is instead of :id I used :class, because it may happen that you have more than one date field
lastly, you should add this on the view
<script>
$( document ).ready(function() {
$('.datepicker').datepicker({format: 'dd/mm/yyyy'});
});
</script>
I'm new on ROR and
making some front-end html codes to rails form helper codes.
For example, this is the html code,
<input id="username" type="text" placeholder="Username" autofocus required>
If i want to make this to rails form helper code it will be like this,
<%=form.text_field :input, :placeholder => 'Username', :autofocus=>true%>
The problem is, it is really inconvenient translating from html code to rails code.
I don't know the attributes for the rails code, so i had to google all the attributes one by one(ex> rails form.text_field autofocus required) and this is making me developing really slow.
Is there are good api site like w3schools for checking the whole attributes or option inside here?
Both links below are from official ruby on rails site
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
http://guides.rubyonrails.org/form_helpers.html
if you simply want to make same input field you need to use this syntax
<%= text_field_tag :username, :placeholder => "Username", :autofocus => true %>
If you use form.text_field in that case rails generate html tag with your object's class for which you have created form_for like:
<% form_for Blog.new do |form| %>
<%= form.text_field :username, :placeholder => 'Username', :autofocus=>true %>
<% end %>
It will generate like this:
<input id="blog_username" name="blog[username]" type="text" placeholder="Username" autofocus required>
Apart from the above ones, this one is also good for the starters
http://www.tutorialspoint.com/ruby-on-rails/rails-html-forms.htm
in Ruby on Rails: What is the difference between text_area and text_area_tag helpers?
More importantly, I would like to know which one is more suited to long HTML text input (specifically in my case, blog posts) ??
Difference is that if you use form_for, pass ActiveRecord object to it and pass, let's say, f to block, it is much more convenient to use for example
<%= f.text_area :body %>
because it sets proper id, name and value automatically.
There's no difference between these helpers in handling long HTML text inputs, but if you want to use it for ActiveRecord object form, you should use text_area because, as I said, it's more convenient.
There are two types of form helpers: those that specifically work with model attributes and those that don‘t.
Ref text_area which specifically work with model
text_area(:post, :body, :cols => 20, :rows => 40)
this will create following html
<textarea cols="20" rows="40" id="post_body" name="post[body]">
#{#post.body}
</textarea>
Ref text_area_tag which doesn‘t rely on an Active Record objec
text_area_tag 'post'
will create following
<textarea id="post" name="post"></textarea>
text_area set tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object)
text_area(:post, :body, :cols => 20, :rows => 40) generate:
<textarea cols="20" rows="40" id="post_body" name="post[body]">
#{#post.body}
</textarea>
And text_area_tag 'post' generate
<textarea id="post" name="post"></textarea>
For more information, look:
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/text_area_tag
http://apidock.com/rails/ActionView/Helpers/FormHelper/text_area
<%= f.text_area :attribute_name %>
<%= text_area_tag :any_name, :any_value %>
if you use form_for (always recommended) to render form then use
<%= f.text_area ....
otherwise you have to use
<%= text_area_tag ....
either will serve the same and no impact on input data (text) size
I'd like to generate check box with custom html attributes (to use UJS later). Here is my view code
<%= check_box_tag "data-toggle-completed" => "" %>
it gives me
<input id="__data-toggle-completed______" name="{"data-toggle-completed"=>""}" type="checkbox" value="1">
But I wanted
<input type="checkbox" data-toggle-completed="">
How can I achieve this?
You must give the custom attributes as fourth arguments, options. The first three arguments are name, value = "1", checked = false. See check_box_tag.
The code could be like this:
<%= check_box_tag :name, 1, false, data: { "toggle-completed" => "" } %>
I was using stimulus js so for custom data-action attribute I did the following
<%= check_box_tag :select_shipping_address, true , true, data:{action:"change->form#show_form"}%>