为什么Rails无法呈现.js.erb文件? [英] Why is rails not rendering the .js.erb file?

查看:116
本文介绍了为什么Rails无法呈现.js.erb文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序,试图将jQuery与HTML文件一起呈现.这样做的重点是jQuery是特定于页面的,因此我不希望通过将其放在页眉中来为每个页面加载它.这是我对文件所做的

I have a rails application where I am trying to have jQuery render WITH an HTML file. The point of this is that the jQuery is specific to the page so I don't want it loading for every page by putting it in the header. Here's what I have done to the files

messages_controller.rb

 # GET /messages/new
 def new
    @message = Message.new
    @users = User.where('id != ' + current_user.id.to_s)
    #if @message.save
       #MessageMailer.new_message(@message).deliver

    respond_to do |format|
       format.html # new.html.erb
       format.js
    end
    #end
 end

/messages/new.html.erb

<div class="row">
    <div class="large-12 columns">
        <div class="panel">
            <h1>New message</h1>
            <%= render 'form' %>

            <%= link_to 'Back', messages_path, :class => "button small" %>
        </div>
    </div>
</div>

/messages/new.js.erb

$("h1").html('hello'); //for testing that it works

我必须添加到另一个文件中吗?我必须要发出一个请求,以便它同时加载HTML和.js吗?

Is there something that I have to add to another file? Is there a request I have to make so it loads both the HTML and .js?

推荐答案

为什么这行不通?

我不认为您的方法是实现所需目标的特别标准方法.未加载JS模板的原因是,如果浏览器请求HTML,则将控制器设置为使用HTML模板进行响应;如果浏览器请求JS,则将其设置为使用JS模板进行响应.它永远不会返回两者,因为浏览器只能请求一个.

Why this won't work?

I don't think your method is a particularly standard way to accomplish what you want. The reason why your JS template is not being loaded is because your controller is setup to respond with an HTML template, if the browser requests HTML, and a JS template, if the browser requests JS. It will never return both because the browser can only request one or the other.

换句话说,控制器中的response_to块指示控制器如何响应对不同类型内容的请求,而不是针对每个请求列出要发送的所有内容类型.

In other words, the respond_to block in your controller is instructing the controller how to respond to requests for different types of content, not listing all of the types of content that will be sent, on every request.

这里有几件事情要考虑.首先,仅因为未在特定页面上使用JS本身就不是很好的理由不加载它. Rails通过Sprockets将所有JS合并并缩小到一个文件中.只要该文件没有更改,它就会被用户的浏览器缓存.当您的用户访问确实需要此JS的页面时,这将导致更快的响应,因为它不需要发出额外的请求即可获取新的JS.

There are a couple of things to consider here. Firstly, just because JS is not used on a particular page, is not, in itself, a good reason not to load it. Rails, via Sprockets, will concatenate and minify all of your JS into a single file. As long as this file does not change, then it will be cached by a user's browser. This will result in a faster response when your user visits a page that does require this JS, as it won't need to make an extra request in order to get the new JS.

如果您担心JS在不应该运行的内容上运行,那么您应该通过在要运行它的一个或多个页面上添加唯一标识符,然后检测该标识符,来真正地防止这种情况的发生. JS中的标识符.例如:

If you are concerned about your JS running on content that it shouldn't, then you should really be guarding against this by adding a unique identifier to the page, or pages, on which you want it to run, then detecting that identifier in your JS. For example:

$("h1.special").html('hello');

解决方案2:使用content_for

但是,在某些情况下,单独加载JS可能会有所帮助.我认为,实现此目标的最佳方法是拥有一个内容块,以便在单个视图需要时将其追加到您的头部.

Solution 2: Use content_for

However, there are some circumstances in which it might be beneficial to load JS separately. The best way to accomplish that, in my opinion, is to have a content for block that appends content to your head, if an individual view needs it.

因此在您的应用程序布局中:

So in your application layout:

#layouts/application.html.erb
<head>
  #Other head content here

  <% if content_for?(:head) %>
    <%= yield(:head) %>
  <% end %>

  #Other head content here
</head>

#messages/new.html.erb
<% content_for :head do $>
  <%= javascript_include_tag "messages_new.js" %>
<% end %>
#Other view stuff here

#assets/messages_new.js
$("h1").html('hello');

messages_new.js将仅包含并因此在包含该content_for块的视图上运行.它不应出现在任何其他页面上.

messages_new.js will then only included, and therefore run, on views where you include that content_for block. It should not appear on any other page.

或者,如果您不关心JS是否最后加载,则只需在new.html.erb的底部添加包含帮助器即可.

Alternatively, if you don't care if the JS is loaded last, then you can just add the include helper at the bottom of the new.html.erb:

#messages/new.html.erb
#Other view stuff here
<%= javascript_include_tag "messages_new.js" %>

这篇关于为什么Rails无法呈现.js.erb文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆