Rails js.erb文件未执行
Rails js.erb file not being executed
我只是想让我的index.js.erb文件执行警报("hi")命令,但它不起作用。我对铁路很陌生,我想知道你们能不能帮我一下!servers_controller.rb索引方法似乎没有正确执行format.js。有什么建议/想法吗?
服务器控制器.rb
def index
@servers = Server.all
update_all_servers #calls the method update_all_servers in application_controller.rb
respond_to do |format|
format.html # index.html.erb
format.json { render json: @servers }
format.js #index.js.erb
end
end
index.js.erb
alert("hi");
$("#comments").fadeOut();
index.html
<%- model_class = Server.new.class -%>
<div class="page-header">
<h1><%=t '.title', :default => model_class.model_name.human.pluralize %></h1>
</div>
<table class="table table-striped">
<thead>
<tr>
<!-- <th><%= model_class.human_attribute_name(:id) %></th> -->
<th><%= model_class.human_attribute_name(:hostname) %></th>
<th><%= model_class.human_attribute_name(:port) %></th>
<!-- <th><%= model_class.human_attribute_name(:username) %></th>
<th><%= model_class.human_attribute_name(:password) %></th>
<th><%= model_class.human_attribute_name(:ssh_username) %></th>
<th><%= model_class.human_attribute_name(:ssh_password) %></th>
<th><%= model_class.human_attribute_name(:source_branch) %></th> -->
<th><%= model_class.human_attribute_name(:source_revision) %></th>
<th><%= model_class.human_attribute_name(:release) %></th>
<th><%= model_class.human_attribute_name(:rhel_version) %></th>
<th><%= model_class.human_attribute_name(:gpu_type) %></th>
<th><%= model_class.human_attribute_name(:total_users) %></th>
<th><%= model_class.human_attribute_name(:current_users) %></th>
<th><%= model_class.human_attribute_name(:created_at) %></th>
<th><%=t '.actions', :default => t("helpers.actions") %></th>
</tr>
</thead>
<tbody>
<% @servers.each do |server| %>
<tr>
<!-- <td><%= link_to server.id, server_path(server) %></td> -->
<td><%= server.hostname %></td>
<td><%= server.port %></td>
<!-- <td><%= server.username %></td>
<td><%= server.password %></td>
<td><%= server.ssh_username %></td>
<td><%= server.ssh_password %></td>
<td><%= server.source_branch %></td> -->
<td><%= server.source_revision %></td>
<td><%= server.release %></td>
<td id="comments"><%= server.rhel_version %></td>
<td><%= server.gpu_type %></td>
<td><%= server.total_users %></td>
<td><%= server.current_users %></td>
<td><%=l server.created_at %></td>
<td>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_server_path(server), :class => 'btn btn-mini' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
server_path(server),
:method => :delete,
:confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
:class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to t('.new', :default => t("helpers.links.new")),
new_server_path,
:class => 'btn btn-primary' %>
application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require_tree .
将浏览器指向/servers.js
,它会显示js,但不会运行,因为它不在脚本标记中。
respond_to
部分只选择一种媒体类型来响应,因此当您查看html页面时,如果您希望包含一些javascript,它需要以某种方式位于脚本标记中的html页面中。index.js.erb格式适用于仅为javascript输出而生成的请求,例如ajax请求。
我知道这个问题很老了,尽管这是解决方案,因为我今天两次偶然发现这个问题,甚至都不记得了;)
根据这篇博客文章,您应该检查您的request type
。必须是JS
。如果您通过AJAX
请求控制器操作,请忽略dataType-attribute
。这将把请求类型设置为*/*
,但它仍然可以工作。
示例:
$.ajax({
url: '/users/auth/facebook/callback',
type: 'GET'
});
编辑
对控制器的请求应该是这样的:
Processing by Users::OmniauthCallbacksController#facebook as JS
用我的解决方案,它看起来像这样:
Processing by Users::OmniauthCallbacksController#facebook as */*
将dataType设置为"JS"对我不起作用,但它仍然可以像JS一样工作。
相关文章:
- 无法执行文件.退出Acrobat PDF中的操作
- 只是 JQuery 可执行文件的一部分.当直接粘贴到HTML中时,一切正常(window.location)
- 节点可执行文件中返回了错误的pid
- 如何从浏览器运行可执行文件
- 在全局范围内执行文件,而不是返回导出的对象
- 获取块上的可执行文件标准输出
- 关于使用 Java 或 JavaScript 安装可执行文件的网站
- 如何将字符串参数从 HTML(使用 JavaScript)传递到 Java 可执行文件 Jar
- 要求JS在加载/解析依赖项之前执行文件代码
- Javascript - 如何执行位于 C: 中的可执行文件
- 如何在Visual Studio 2012 for Windows 8中使用JavaScript运行外部可执行文件
- 使用PHP'将网站打包为Windows可执行文件;的内置服务器
- 在gullowwatch上运行可执行文件
- 如何在我的Javascript中执行二进制可执行文件
- 如何使用Node.js在客户端运行可执行文件
- Apache/XAMPP for Windows错误地将文件视为可执行文件
- 如何在本地网络上正确执行C#文件
- 如何将node-js控制台应用程序打包为可执行文件
- 如何创建Javascript可执行文件
- 无法在Nodejs中执行文件上传程序