Rails js.erb文件未被执行

时间:2012-07-19 20:31:30

标签: javascript ruby-on-rails ruby ajax

我只是想让我的index.js.erb文件执行一个警告(“hi”)命令,但它无法正常工作。我对rails非常陌生,我想知道你们是否可以帮助我!看起来servers_controller.rb索引方法没有正确执行format.js。有什么建议/想法吗?

servers_controller.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 .

2 个答案:

答案 0 :(得分:2)

将您的浏览器指向/servers.js,它将显示js,但不会运行,因为它不在脚本标记中。

respond_to部分选择一种且只有一种媒体类型进行响应,因此当您查看html页面时,如果您想要包含一些javascript,则需要在脚本标记的html页面中不知何故。 index.js.erb格式适用于仅针对javascript输出的请求,例如ajax请求。

答案 1 :(得分:1)

我知道这个问题已经过时了,虽然这是解决办法,因为我今天两次偶然发现了这个问题,甚至没有记住;)

根据this博文,您应该查看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一样工作。