I have a date in one of my models, which I am currently rendering in my simple form with
<%= f.input :issue_date, label:false %>
This, I believe automatically applies the date_select helper, causing it to render three very nice selection fields: one for day, one for month one for year.
The problem is that I really can't stand using select for the day and year (just seems more efficient to type it). Is there a good way to change the rendering of these fields? I really like how convenient this automatic rendering is; I'd just like it to render with one select box and two text boxes. Thanks for any suggestions.
Related
I just need 1 instance where I have a date field instead of a datetime field. No need in creating a new column just for 1 date.
How do I convert datetime to date when displaying data?
HTML that needs converting;
<td> <%= product.pDate %> </td>
There are actually a number of approaches you can take in Rails. Link Nguyen's answer is the pure Ruby approach, which works here and would also work in other Ruby contexts outside of Rails.
Rails also adds #to_formatted_s and #to_s (they're aliases of one another) to the date and time objects, e.g.:
<%= product.pDate.to_s(:long) %>
Rails define some date formats by default, and you can add your own by adding values to the Date::DATE_FORMATS array in an initializer (see the docs for examples).
A additional, slightly newer approach uses the locale files (e.g., config/locales/en.yml) to define formats, which can then be accessed in your views using the l (for localize) helper:
# Your ERb file
<%= l product.pDate, format: :short %>
# Your configs/locale/xx.yml file
# NB: `short` is defined by default but you can redefine it if you want
en:
date:
formats:
short: "%m/%d/%y"
See the Rails i18n guide for more.
The advantage of both Rails approaches is that you can decide on a standard style, or collection of standard styles, that you apply to dates/times everywhere in your app. If you were to decide to switch from two-digit years to four, or vice versa, the strftime approach would require editing every view (and not missing any), but with either Date#to_s or l() you change the configuration format in one place. The i18n approach allows you to further customise date formats for international markets if you want to (for example, day/month/year is an almost exclusively US ordering, so you might want to use a different format for other languages).
You can try strftime. for example product.strftime("%m/%d/%Y")
I have a form which has a field of type Date.
The following line creates an input for the field, where the user can choose the date.
<%= f.date_select :start_date %>
How can I make it display only weekdays as input options?
It's not possible to do what you want with a date_select helper. You have two alternatives (and one isn't really an alternative):
1) Write a validation in the model that fails if the date selected is not a weekend; or
2) Use a JavaScript date picker (like jQuery DatePicker), which is the only real solution. You can disable specific days or categories of days with DatePicker.
I want to use chartkick to create a line graph of records, which belong to a planned task.
In other words: plantask has_many records
Records has two fields I'm interested in graphing. The created_at, which will be my X-axis, and data(An integer), which will be my Y-axis.
So far I've gotten pretty close. By inserting this into my view:
<%= line_chart #plantask.records.group_by_day(:created_at).sum(:data) %>
I can see that my x-axis is displaying perfectly. However, it appears that the y-axis is not loading the records :created_at field, but is loading the :created_at from within the plantask model. (All of my records are mapped to yesterday at 7:00pm) This seems strange to me. Any hints on what I've messed up? Thanks you guys.
It turns out that I was approaching this problem the wrong way. Group by day with sum combines every task into one, and adds the value. What I really needed was this:
<%= line_chart #plantask.records.group(:created_at).sum(:data) %>
I'm using Rails 3.2.5 and when it return a value o :price for editing it just brings one decimal, eg. 600.0, i need that i bring 2 decimals (eg. 600.00) in database is recorded 600.00, in my locales i already set for 2 decimals, and still dont work.
I tried
number_to_currency(:price, :precision => 2)
but it works well for a view like "show", i need that it return the propper value for editing, on textfield.
in my migration the field "price" is set do decimal(15,2).
Can someone help?
Thank You!
The text fields are operating on the "native" value of the field, as it is handled by ActiveRecord, not as it is stored in the database.
Currency fields are tricky since ActiveRecord is translating between the database representation and the ruby/rails representation, a BigDecimal.
If your goal is that you want to show your users $1,000.00 and enable them to edit it, some ideas:
Use the Money gem Ylan S refers to.
Use an Edit in place widget Eg screencast. You'd use number_to_currency to display the value. When clicked, the input field would show the value without the currency symbol, commas for thousands separators, etc. Note that this is how Excel works: when you edit the value of a currency field, you don't enter 1,000. You enter 1000.
In my experience, instead of using decimal for storing currency, it's best to store the amounts in cents, as an integer. This will take care of multiple problems, including the one you are having now.
I have had much success in the past using the Money gem and its companion money-rails.
Issue solved using 'delocalize' gem.
Old but gold! :) Thank you all!
The simplest way to have your text_field display a formatted value is to pass it explicitly, like this:
f.text_field :price, :value => number_to_currency(:price, :precision => 2)
You'll need to interpret (and possibly re-format) the value in the controller method that handles the form.
See How can I format the value shown in a Rails edit field?
I'm a new ruby on rails coder who is trying to create a neatly displayed calendar of the week for students to check their timetables.
I have stored the lessons in a table with the following data:
:course_id, :state_unit_code, :day_of_week, :start_date, :end_date, :start_time, :end_time, :classroom_id, :campus_id, :lecturer_id
I wish to take the data from that table and transform it into an html table similar to that shown:
I've mocked it up using table, tr td rowspan colspan and so on.
If you can point me in the right direction I'd be most appreciable.
Follows on from a post by another user (allesklar): How would you build this daily class schedule?
I would recommend using a gem for this unless you are a strong confident rails programmer with some decent experience. It's a good project if you're just doing it to learn but not so much if you actually want it to be used as a 'production app. in the real world.
I think that https://github.com/elevation/event_calendar might meet your needs. Take a look and see.
You may also need to use a separate gui date picker at some point and their are many solutions for that such as http://code.google.com/p/calendardateselect/ though this is just about picking dates, not the full calendar display of event also. But it could be handy. You'll also see 30 (!) different date pickers here: http://www.1stwebdesigner.com/freebies/jquery-calendar-plugins/ that also include ones that let you span dates.