2012-07-19 26 views
7

मैं बस अपनी अनुक्रमणिका.js.erb फ़ाइल को एक चेतावनी ("हाय") कमांड निष्पादित करने की कोशिश कर रहा हूं, हालांकि यह काम नहीं कर रहा है। मैं रेल के लिए बहुत नया हूं, और मैं सोच रहा था कि क्या आप मेरी मदद कर सकते हैं! ऐसा लगता है कि server_controller.rb अनुक्रमणिका विधि format.js को सही तरीके से निष्पादित नहीं कर रही है। कोई सुझाव/विचार?रेल 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' %> 

/servers.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

यदि आप दूसरी पंक्ति को हटाते हैं तो जावास्क्रिप्ट चलाता है? केवल अलर्ट को ही चलाने का प्रयास करें। – cdesrosiers

+0

दूसरी पंक्ति को हटाने के लिए अभी भी काम नहीं करता है :( – Rahul

+0

और आप सुनिश्चित हैं कि इंडेक्स एक्शन को भी बुलाया जा रहा है? – cdesrosiers

उत्तर

2

प्वाइंट आपके ब्राउज़र application.js और यह js दिखाई देगा, लेकिन के रूप में यह एक स्क्रिप्ट टैग में नहीं है नहीं चलेंगे।

respond_to अनुभाग जवाब देने के लिए एक और केवल एक मीडिया प्रकार चुनता है, इसलिए जब आप किसी HTML पृष्ठ को देख रहे हों, यदि आप कुछ जावास्क्रिप्ट शामिल करना चाहते हैं, तो इसे किसी भी तरह से किसी स्क्रिप्ट टैग में HTML पृष्ठ में होना आवश्यक है। Index.js.erb प्रारूप उन अनुरोधों के लिए है जो पूरी तरह से जावास्क्रिप्ट आउटपुट के लिए किए जाते हैं, जैसे AJAX अनुरोध।

+0

धन्यवाद, मैं इसे आज़मा दूंगा! – Rahul

+0

तो क्या विधि विधि के लिए एक method.js फ़ाइल है जिसमें रूली कोड <% %> रेल 4 में भी शामिल है? आपके सामने एक्सटेंशन में .erb जोड़ने की आवश्यकता है – Rubytastic

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 */* 

डेटा टाइप करना 'जेएस' टाइप करना मेरे लिए काम नहीं करता है, लेकिन यह अभी भी काम करेगा जैसे कि यह एक जेएस था।

संबंधित मुद्दे