简体   繁体   English

在erb标签内调用javascript函数

[英]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: 我试图将行添加到表中,如下面的view.html.erb文件中的代码片段所示:

<% @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. 我遇到的问题是startDate和endDate值以以下格式显示:Thu May 01 2014 08:00:00 GMT-0400(EDT),我要在其中以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. 我有一个JavaScript函数formatDate,它将更改格式,但我想不出一种从erb标签中调用它的方法。

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 :) (为了清楚起见,省略了不相关的标记;请考虑在以后的问题中进行同样的操作。如果与问题不直接相关,则只是噪音:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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