In Rails' forms, date select drop-downs are easy:
<%= f.label :my_date %>
<%= f.date_select :my_date %>
Then I get 3 select drop-downs for the year, month and day. Is there an easy way to add HTML between these select elements without using JavaScript? I want to be able to style and label them nicer.
Convert it to a text field and add a JQuery datepicker or similar.
Theres a good guide here, http://railscasts.com/episodes/213-calendars
You can easily style them with CSS as each part of date_select is postfixed with '_1i', '_2i' etc . For a date_select with id 'my_date' you can use
#my_date_1i // the year
#my_date_2i // the month
#my_date_3i // the day
Hope this helps
Cheers
Related
I'm really new to Rails and I'm using v6.1.4. In my application I'm using I18n localization. At other places for view-only label dates(not using BIP) I use
<%=l #client.active_date.to_date %>
and it works. However, in Best-In-Place there is a different syntax.
<span class="date-container"><%= best_in_place task, :due_date, :as => :date, class: 'hyperlink' %></span>
This causes the dates to be displayed in the yyyy-mm-dd format throughout. I want the dates to be formatted according to the locales currently in my application. I'm using the JQuery datepicker, but it is activated after I click on the date, and the format is changed to what I have set on the jquery setDefaults option.
How do I tackle this?
I think this post addresses your question.
You could add a display_as: :formatted_date to your method call and define that formatted_date method in your model. From there you should be able to format dates as needed.
I have a search built on date fields. I want to use the date_field instead of date_selector if possible.
The date field works fine in chrome. However, in IE 11, the placeholder and controls do not work. The date select does work in IE.
<%= f.date_field( :req_resolution_from_date) %>
<%= f.date_select( :req_resolution_to_date) %>
In chrome, both date_field and date_select work.
However, in IE, only the date_select works
Is there any way to get the date_field to show in IE with the date controls?
I am working on a web app form and don't really know anything at all about rails. I mainly tweak html and css while the real devs do the hard work! But I am now trying to simply add a dollar sign - '$' to an input box that is created using rails. I have no clue how to do this and my google efforts have turned up fruitless.
this is the code for the input box
` <%= f.label :incomeAnnual, 'Annual income' %>
<%= f.text_field :incomeAnnual, tabindex: 25 %>
<br/>'
can anyone please help me?
To add the dollar sign you can use the value attribute as specified. Now to change it into currency format, if it is a predefined value you can use the rails helper number_to_currency. More details:
http://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_currency
But if you need to dynamically ad the commas as the user enters the value in text box then you need to use javascript for it. You can use a number of ways for it:
Add commas for a number in input field while typing
How can I format numbers as money in JavaScript?
Also there is a simple method which I prefer i.e., toLocaleString() which can be applied to a number. So what you need to do is on keypress() you need to take the number and convert it into the format you need and put in the text box. Hope this helps.
Assuming you want the $ to appear inside the text box, you could use either :value or :placeholder options on the text_field.
A placeholder will go away as the user starts typing, so you might prefer a value here.
For example:
:value=> '$'
or
:placeholder => '$'
See also
http://apidock.com/rails/ActionView/Helpers/FormHelper/text_field
You can use
<%= f.text_field :incomeAnnual, :value=> "$ #{f.object.incomeAnnual}",tabindex: 25 %>
Use the number_to_currency function. For example
<%= number_to_currency item.total_price %>
Given a standard select code:
<%= f.select :type_name, [['Genomics','Genomics'],['Proteomics','Proteomics'],['Transcriptomics','Transcriptomics'],['Other','Other'] %>
Can someone explain how I would go about creating a text field when 'Other' is selected? So that the type_name can be something other than the options in the select?
I realise this is a simple question, but I haven't found a concise answer as of yet!
There are lots of ways to do this, but they all require JavaScript. The general approach I like is to put a hidden text field in the form, then attach a JavaScript event handler to the select tag that shows the field when the "Other" option is selected.
Here's a gist of the script I typically use for this. It handles the JavaScript binding using data attributes. Add the script to your assets, then put something like this in your form:
<%= f.select :type_name, [['Genomics','Genomics'],['Proteomics','Proteomics'],['Transcriptomics','Transcriptomics'],['Other','Other'] %>
<%= f.text_field :type_name_other, "data-depends-on" => "#object_type_name", "data-depends-on-value" => "Other" %>
where #object_type_name is the HTML id of your dropdown.
You need to create an attr_accessor on the model f is attached to (like type_name_other), add a text_field to the form below the select for type_name_other in a div that is initially hidden (in CSS: display:none), then create a javascript listener that detects when the select form has changed and if the selected ansser is "other" show the hidden field else hide it. You will then need to see if type_name_other has a value when processing the form and use it if so.
I'm using a date select in a rails 3 form.
<%=f.date_select :date %>
I would like to restrict the dates so that you can only pick dates that fall on a Sunday. Is there any way of going about doing this?
I'm also trying to stop dates which have already passed from appearing.
Thanks for any help in advance!
Rails date_select field generates three dropdown to select the parts of the date. There is no chanche, that you modify for example the month, and the day will still be sunday.
You must write some js magic to enforce such a role, or find an already existing datepicker and limit it. Or alternatively, you let the user to select a week, and calculate the exact date of sunday from that.
Ok having studied this out a bit further I don't think this is possible due to the format of the date_select field. The closest I can get is
<%=f.date_select :date, start_year: Time.now.year %>
so that at least you can't select dates from previous years. I've implemented the restriction on days and months that have past by setting up the view to automatically delete records that aren't relevant:
<% if(service.date < Date.today) %>
<% service.destroy %>
<% end %>
Not perfect but does the job in my case.