2012-07-19 55 views
7

我只是想讓我的index.js.erb文件執行一個alert(「hi」)命令,但它不工作。我對軌道很陌生,我想知道你們是否可以幫助我!它看起來像servers_controller.rb索引方法沒有正確執行format.js。任何建議/想法?Rails js.erb文件沒有被執行

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 . 
+0

如果刪除第二行,JavaScript是否運行?嘗試自行運行警報。 – cdesrosiers 2012-07-19 20:36:35

+0

刪除第二行仍然不起作用:( – Rahul 2012-07-19 20:40:31

+0

而且你確定索引操作甚至被調用? – cdesrosiers 2012-07-19 20:43:19

回答

2

瀏覽器指向/servers.js,它會顯示的JS,但將無法運行,因爲它不是在一個腳本標籤。

respond_to部分選擇一個且只有一個媒體類型進行響應,因此當您查看html頁面時,如果您希望包含某些javascript,它需要以某種方式在腳本標記中的html頁面中。 index.js.erb格式適用於僅用於javascript輸出的請求,例如ajax請求。

+0

謝謝,我會試試這個! – Rahul 2012-07-20 13:56:48

+0

因此,def method的method.js文件在rails 4中還包括ruby代碼<% %>?在你需要在擴展中添加.erb之前 – Rubytastic 2013-10-16 10:24:51

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一樣工作。