2012-07-19 24 views
7

Tôi chỉ đơn giản là cố gắng để có được tập tin index.js.erb của tôi để thực thi một lệnh cảnh báo ("hi"), tuy nhiên nó không hoạt động. Tôi khá mới với đường ray, và tôi đã tự hỏi nếu các bạn có thể giúp tôi! Có vẻ như phương thức index_controller.rb không thực thi đúng định dạng. Bất kỳ đề xuất/ý tưởng?Rails tập tin js.erb không được thực hiện

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

Liệu javascript có chạy nếu bạn xóa dòng thứ hai không? Hãy thử chỉ chạy cảnh báo của chính nó. – cdesrosiers

+0

xóa dòng thứ hai vẫn không hoạt động: ( – Rahul

+0

Và bạn chắc chắn rằng hành động chỉ mục thậm chí còn được gọi? – cdesrosiers

Trả lời

2

Chỏ trình duyệt web của bạn để /servers.js và nó sẽ hiển thị các js, nhưng sẽ không chạy vì nó không có trong một thẻ script.

Phần respond_to chọn một và chỉ một loại phương tiện để phản hồi, vì vậy khi bạn đang xem trang html, nếu bạn muốn bao gồm javascript, nó cần phải nằm trong trang html trong thẻ tập lệnh bằng cách nào đó. Định dạng index.js.erb dành cho các yêu cầu được tạo ra chỉ cho đầu ra javascript, chẳng hạn như các yêu cầu ajax.

+0

Cảm ơn bạn, tôi sẽ cố gắng! – Rahul

+0

Vì vậy, có một tập tin method.js cho phương pháp def làm việc với cũng bao gồm mã ruby ​​<% %> trong đường ray 4? trước khi bạn bắt buộc phải thêm .erb vào phần mở rộng – Rubytastic

1

Tôi biết câu hỏi này là cũ, mặc dù đây là giải pháp, vì tôi vấp qua vấn đề này gấp đôi ngày hôm nay, thậm chí không cần ghi nhớ;)

Theo this bài đăng blog, bạn nên kiểm tra request type của bạn. Nó phải là JS. Nếu bạn yêu cầu hành động điều khiển đó qua AJAX, chỉ cần thoát khỏi số dataType-attribute. Điều này sẽ đặt loại yêu cầu là */*, nhưng nó vẫn hoạt động.

Ví dụ:

$.ajax({ 
    url: '/users/auth/facebook/callback', 
    type: 'GET' 
}); 

EDIT

Yêu cầu bộ điều khiển sẽ giống như thế này:

Processing by Users::OmniauthCallbacksController#facebook as JS 

Với giải pháp của tôi nó sẽ giống như thế này:

Processing by Users::OmniauthCallbacksController#facebook as */* 

Đặt kiểu dữ liệu thành 'JS' không hoạt động đối với tôi, nhưng nó vẫn hoạt động như thể nó là một JS.

Các vấn đề liên quan