如何在app/views中渲染new.js.coffee.erb?
使用 Rails 3.1 jquery_ujs,我有一个链接:remote => true,控制器新操作以 js 响应并呈现 new.js.erb,其中包含:
$('#post-form').html('<%= escape_javascript(render(:partial => "form")) %>');
它呈现 _form.html.erb 部分。这有效。
现在我想使用 Coffeescript,但将 new.js.erb 重命名为 new.js.coffee.erb 不起作用。资产管道仅适用于应用程序/资产吗?我做错了什么?谢谢。
Using Rails 3.1 jquery_ujs, I have a link with :remote => true, and the controller new action responds with js and render new.js.erb which contains:
$('#post-form').html('<%= escape_javascript(render(:partial => "form")) %>');
It renders the _form.html.erb partial. This works.
Now I want to use Coffeescript, but renaming new.js.erb to new.js.coffee.erb doesn't work. Does the asset pipeline only work in app/assets? What am I doing wrong? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在使用 Rails 3.1.0 时遇到了同样的问题。尝试将文件重命名为
new.js.coffee
。尽管文件名没有扩展名,它仍然应该呈现 erb。视图模板不遵循与资产管道相同的约定,这绝对令人困惑。
I had this same issue using Rails 3.1.0. Try renaming your file to just
new.js.coffee
. It should still render erb despite not having the extension on the filename.It's definitely confusing that view templates don't follow the same conventions as the asset pipeline.
如果您希望保留
.js.coffee.erb
扩展名,这里有一段 Rails 4 的代码,可以让 Rails 将该文件识别为有效的视图模板:(这是一个贡献 由 cervinka 发表于 Coffee-rails 问题 #36)
If you wish to keep the
.js.coffee.erb
extension here's a piece of code for Rails 4 to have Rails recognize the file as a valid view template:(This is a contribution by cervinka on coffee-rails issue #36)