简体   繁体   中英

Call javascript function inside erb tag

I'm trying to add rows to a table as shown in the below code snippet from my view.html.erb file:

<% @res.each do |r| %>
    <tr>
        <td class="alt" id="resName"><%= r.name %></td>
        <td class="norm" id="startDate"><%= r.startDate %></td>
        <td class="norm" id="endDate"  ><%= r.endDate %></td>
    </tr>
<% end %>

The problem that I am encountering is that the startDate and endDate values are being displayed with the format: Thu May 01 2014 08:00:00 GMT-0400 (EDT) where I want to display them with the format 05/01/2014.

I have a javascript function, formatDate, which will make the formatting change, but I can't figure out a way to call it from within the erb tags.

I've tried a few things, but can't seem to get it to work. Is it not possible, or am I just doing something wrong? Can anyone offer any help/advice? Thank you in advance!

<td><%= r.startDate.strftime("%-m/%-d/%Y)" %></td>

I prefer very light views; I'd use a decorator:

<td><%= r.start_date_formatter %></td>

Or at least a helper:

<td><%= view_date(r.start_date) %></td>

(Unrelated markup elided for clarity; consider doing the same in future questions. If it's not directly related to the question, it's just noise :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM