Conditional time display - ruby-on-rails

In the mailbox section of my Rails app, I simply display the timestamps of each message as follows:
<%= message.created_at.utc %>
But I want to change it so that if the message's date is different to today, then display the date, otherwise just display the time (e.g. like Gmail).

Try putting something like this in a helper:
def time_or_date(date)
date.today? ? date.strftime('%H:%M') : date.strftime('%d/%m')
end
You can then call it from your view like this:
<%= time_or_date(message.created_at.utc) %>
You might want to change strftime so it fits your needs:
http://www.ruby-doc.org/core/classes/Time.html#M000297

Related

Rail 4 image_tag with variable

I am trying to make a show page that displays an image based on the person called out of the database. My current code looks like this :
<%= image_tag('DAF_520x222.jpg') %>
I want to exchange the 'DAF_520x222' portion for a call of #user_name but I am unsure of how to go about putting this in the image_tag itself.
You can directly interpolate it into the string. It will automatically change it to its value
<%= image_tag("#{#user_name}.jpg") %>
This should work

Show a date variable from database - Rails

I have an attribute in my user database of type "Date".
I want to show it in the "show view", in any format.
What shall I write in show.html.erb so that I can see the contents of this field of a user object ?
Thanks :)
You might want to store the date format in a config file and/or a helper so you can easily modify it and use it globally wherever you are printing it in a view.
You can print a date like this:
<%= #user.datecolumn.strftime('%B %d, %Y') %>
See this article where the date formats are listed and there is more information on the subject:
http://www.linux-mag.com/id/4070/
<%= user.created_at.strftime("%B %m") %>
Take a look at the strftime method: http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime

Rails 3.1 Filter 'Created_At' field

in my Rails 3.1, ruby 1.9.2 application, I've an Object 'Post' that has several fields such as id,created_at,updated_at,name,description.... In my view I want show post.created_at field filtered by date: I explain better:
In my view I'd want something such as:
but html must render something such as: 2011-30-06 and not 2011-30-06 11:04 am
in other words I want display only date and not hour, how can I do?
You should use strftime to format correctly the date (here's the documentation)
It should look like this <%= post.created_at.strftime("%Y-%M-%d) %>
I'd suggest to create method display_date in application_helper.rb, don't forget about DRY!
def display_date(input_date)
input_date.strftime("%Y-%M-%d")
end

How would I add a method to ActiveView::Helpers::FormHelper to be used with form_for?

I am currently making a plugin, and I would like to add a method to ActiveView::Helpers::FormHelper, Essentially, the plugin is a helper that will convert checkbox input into bitwise flags so when you do actions like new and update, you can continue to pass in a params hash, and my plugin will pull out the checkbox data and convert it into a single number representing the flag state. Anyway, I want to be able to do something like this:
<% form_for #person do |f| %>
<%= f.check_boxes_for_flags %>
<% end %>
Which would create checkboxes in the HTML and then set them accordingly to the flags. I know how to add an instance method to ActiveView::Helpers::FormHelper, but I'm not sure how to access #person from this method. Any ideas?
Why wouldn't you use:
<%= f.check_boxes_for_flags :country %>
That way you can create your extension similar to how the ActiveView helpers work.
Take a look at how check_box_tag in the rails source code gets the name from the model. Try to follow the conventions set forth by the framework, it makes things easier for you and those who will maintain your code after you.

ruby on rails form_for

I have a calendar_date_select in a view that shows a table listing all the information on a certain phone. I want to add a To: and From: date range that a user can select and update the table in the view. The structure is like this:
Usage Controller
Detail action in the usage controller that shows the call history from a certain phone.
Inside detail I want the To and from fields with a refresh button.
What is exactly happening in this code:
<% form_for :date_range do |f| %>
<%= f.calendar_date_select :start, :time => true %>
<%= f.calendar_date_select :end, :time => true %>
<%= f.submit "Submit" %>
<% end %>
Does this pass a hash to the usage controller and look for a date_range method? My current route looks like this
usage/detail/id-of-phone
I would like it to look something like this:
usage/detail/id-of-phone#start-end
So I could then (I think) extract the start and end dates from the params with just params[:start] and params[:end]. Am I doing this right, or is there a better way to get the desired result that I want.
I haven't used the calendar_date_select plugin, but you should be getting the parameters back already.
params[:date_range][:start]
params[:date_range][:end]
What you want is the url or the smart solution to get the params?
Please set the routes.rb for the url. Or you can make many method in the 'DataRange' model.
As many programmers using, save many dates in the model. But making us terrible is using the params smartly.
Such as
class Model
def start
......
end
def end
......
end
end
You can't get the params by params[:start] if you pass the params by the form. You can see the form's html for detail.
Please use the
params[:...][:start]

Resources