Question

I'm passing a simple object into my RABL view, the rabl view is basically two lines:

object @invitations
attributes :email, :active, :invitation_sent_at, :invitation_accepted_at

How can I format the date :invitation_sent_at?

Was it helpful?

Solution 2

You can pass the formatted attribute as a node:

object @invitations
attributes :email, :active
node(:formatted_invitation_sent_at){|invite| invite.invitation_sent_at.strftime(some_format)}
node(:formatted_invitation_accepted_at){|invite| invite.invitation_accepted_at.strftime(some_format)}

Alternatively, make this a helper_method in your model, and pass that helper_method instead of the attribute.

OTHER TIPS

Formatting on the client might be better.

If you use something like AngularJS, the following should work.

<ul ng-repeat="invitation in invitations">

  <li>{{ invitation.invitation_sent_at | date: 'yyyy-MM-dd HH:mm:ss' }}</li>

</ul>

Depending on your use case, you may want to format the date in the application receiving the response rather than in the response itself. This will keep your RABL templates decoupled from the final rendering of the date and thus more easily consumable.

Then assuming you're consuming the response with JavaScript:

// parse manually
var invitedAt = new Date(response.invitation_sent_at); // assumes Rails default UTC formatting
invitedAt.getFullYear(); // 2014
invitedAt.getMonth(); // 1
// etc.

// parse with a library like moment.js (http://momentjs.com/)
moment(response.invitation_sent_at).format('MMMM Do YYYY, h:mm:ss a'); // February 12th 2014, 1:01:59 pm
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top