Rails 控制器何时呈现为 JS、HTML 或“q"? [英] When do Rails controllers render as JS, HTML, or 'q'?

查看:30
本文介绍了Rails 控制器何时呈现为 JS、HTML 或“q"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试为什么一段 HTML 代码无法在(其他人的)Rails 代码中正确呈现.

我注意到当我的 .erb 模板被调用时,控制台输出如下:

在 2014-01-07 18:39:02 +0200 开始 GET "/sub_account/new?_=1389112701545" for 127.0.0.1由 SubAccountController#new 处理为 q=0.5参数:{"_"=>"1389112701545"}渲染 sub_account/new.erb (0.5ms)在 16ms 内完成 200 OK(查看:4.9ms | ActiveRecord:1.7ms)

这不返回任何内容.我从sub_account/new.erb"中删除了所有变量,并放入了一个hello world",但这也没有出现.

这是我的sub_account"控制器的样子:

#encoding: utf-8类 SubAccountController <设计::注册控制器before_filter :authenticate_user!布局主要"response_to :js, :html, :json..定义新构建资源({})response_with self.resource结尾定义资源instance_variable_get(:"@#{resource_name}")结尾私人的def build_resource(hash=nil)self.resource = resource_class.new_with_session(hash || {}, session)结尾定义资源名称'子帐户'结尾定义资源类类::子帐户结尾

查看实际在同一站点上呈现的其他代码,我看到以下内容:

在 2014-01-07 18:38:55 +0200 开始 GET "/admin/driver/new" for 127.0.0.1由 Admin::DriverController#new 作为 JS 处理呈现 admin/driver/_form_fields.erb (11.9ms)呈现 admin/driver/new.erb (79.9ms)在 98 毫秒内完成 200 个 OK(查看次数:84.2 毫秒 | ActiveRecord:1.9 毫秒)

我注意到这被渲染为 JS,在另一个例子中,我看到以下内容:

在 2014-01-07 18:46:28 +0200 开始 GET "/personal/new" for 127.0.0.1由 PersonalController#new 处理为 HTML呈现 shared/_summary_errors.erb (0.2ms)呈现的personal/_hidden_​​fields.erb (1.4ms)呈现的personal/_card.erb (9.8ms)呈现的personal/_form_fields.erb(28.9ms)渲染设计/共享/_links.erb (0.8ms)在 layouts/main 中渲染个人/new.html.erb (104.3ms)渲染布局/_head.erb (43.2ms)渲染 shared/_logo.erb (1.0ms)呈现 shared/_sign_up_header.erb (3.1ms)渲染布局/_header.erb (4.1ms)在 7442ms 内完成 200 OK(查看:168.7ms | ActiveRecord:0.7ms)

所以这被呈现为 HTML.但是,我不知道如何或为什么?我从未被渲染的代码被渲染为 q=0.5,但我不知道这意味着什么或如何更改它.

更新

这是 new.html.erb 的内容(上面的日志说 new.erb 但我同时尝试了两者):

<% content_for :scripts do %><script type="text/javascript">//<![CDATA[window.resource_name = '<%= resource_name %>';//]]><%= javascript_include_tag 'regex-mask-plugin', :profile_page, 'custom_validators', 'custimize_validator', 'steps' %><%结束%><div class="row" style="margin-top: 30px;"><div class="col-md-11"><%= form_for(resource, :as => resource_name, :url => sub_account_index_path, :validate => true) do |f|%><div class="row step"><div class="col-md-12"><%= 渲染 'form_fields', :f =>f, :focus =>真%>

<!--<div class="col-md-2"><div class="row"><div class="col-md-8"><%= f.submit '创建', :class =>'btn btn-默认 btn-block'%>

</div>--><%结束%>

更新 2:

查看 chrome 的开发工具.. 我注意到单击按钮时发出的请求实际上以与其他成功渲染相同的方式返回 html:

但是,请求头是不同的..比较下面的

正确的接受 */*;q=0.5, text/javascript,.. 而失败的接受 q=0.5, text/javascript.... 我怎样才能让它也接受 */* ?

另一个区别是失败的查询字符串参数为 _:1389152333528 .. 会有所不同吗?

更新:我刚刚确认正是这个奇怪的查询字符串导致了所有问题..我在没有查询字符串的情况下在邮递员上测试了相同的请求..我肯定得到了正确的 html:

解决方案

尝试将视图中的文件从 something.erb 重命名为 something.html.erb 和 something.js.erb.当你写
respond_to :js, :html, :json
<代码>...
respond_with self.resource控制器首先尝试渲染 something.js.erb 然后 something.html.erb 等等.没有扩展,它不知道要渲染什么视图.

I'm trying to debug why a piece of HTML code isn't rendering properly in (someone else's) Rails code.

I noticed that when my .erb template is called, the console outputs the following:

Started GET "/sub_account/new?_=1389112701545" for 127.0.0.1 at 2014-01-07 18:39:02 +0200
Processing by SubAccountController#new as q=0.5
  Parameters: {"_"=>"1389112701545"}
  Rendered sub_account/new.erb (0.5ms)
Completed 200 OK in 16ms (Views: 4.9ms | ActiveRecord: 1.7ms)

This returns nothing. I removed all the variables from "sub_account/new.erb" and just put in a "hello world", but that isn't showing up either.

This is what my "sub_account" controller looks like:

#encoding: utf-8
class SubAccountController < Devise::RegistrationsController
  before_filter :authenticate_user!
  layout 'main'
  respond_to :js, :html, :json
  ..

  def new
    build_resource({})
    respond_with self.resource
  end

  def resource
    instance_variable_get(:"@#{resource_name}")
  end

  private
  def build_resource(hash=nil)
    self.resource = resource_class.new_with_session(hash || {}, session)
  end

  def resource_name
    'SubAccount'
  end

  def resource_class
    Classes::SubAccount
  end

Looking at other code that actually renders on the same site, I see the following at one point:

Started GET "/admin/driver/new" for 127.0.0.1 at 2014-01-07 18:38:55 +0200
Processing by Admin::DriverController#new as JS
  Rendered admin/driver/_form_fields.erb (11.9ms)
  Rendered admin/driver/new.erb (79.9ms)
Completed 200 OK in 98ms (Views: 84.2ms | ActiveRecord: 1.9ms)

I noticed this got rendered as JS, in another example, I see the following:

Started GET "/personal/new" for 127.0.0.1 at 2014-01-07 18:46:28 +0200
Processing by PersonalController#new as HTML
  Rendered shared/_summary_errors.erb (0.2ms)
  Rendered personal/_hidden_fields.erb (1.4ms)
  Rendered personal/_card.erb (9.8ms)
  Rendered personal/_form_fields.erb (28.9ms)
  Rendered devise/shared/_links.erb (0.8ms)
  Rendered personal/new.html.erb within layouts/main (104.3ms)
  Rendered layouts/_head.erb (43.2ms)
  Rendered shared/_logo.erb (1.0ms)
  Rendered shared/_sign_up_header.erb (3.1ms)
  Rendered layouts/_header.erb (4.1ms)
Completed 200 OK in 7442ms (Views: 168.7ms | ActiveRecord: 0.7ms)

So this was rendered as HTML. But, I have no idea how or why? My code that never gets rendered is rendered as q=0.5, but I have no idea what that means or how to change it.

update

this is the content of new.html.erb (the logs above say new.erb but i was trying both):

<% content_for :scripts do %>
  <script type="text/javascript">
    //<![CDATA[
    window.resource_name = '<%= resource_name %>';
    //]]>
  </script>
  <%= javascript_include_tag  'regex-mask-plugin', :profile_page, 'custom_validators', 'custimize_validator', 'steps' %>
<% end %>

<div class="row" style="margin-top: 30px;">
  <div class="col-md-11">
    <%= form_for(resource, :as => resource_name, :url => sub_account_index_path, :validate => true) do |f| %>
      <div class="row steps">
        <div class="col-md-12">
          <%= render 'form_fields', :f => f, :focus => true %>
        </div>
      </div>

      <!--<div class="col-md-2">
        <div class="row">
          <div class="col-md-8">
            <%= f.submit 'Create', :class => 'btn btn-default btn-block'%>
          </div>
        </div>
      </div>-->
    <% end %>
  </div>
</div>

update 2:

looking at chrome's dev tools.. I noticed that the request made when click the button does actually return html the same way the other successful renders do:

however, the request headers are different.. compare the below

with

the correct one accepts */*;q=0.5, text/javascript,.. and the failing one accepts q=0.5, text/javascript.... how can I make it accept */* as well?

also another difference is that the failing one has a query string parameter of _:1389152333528.. would that make a difference?

update: i just confirmed that it is this weird query string that's causing all the trouble.. i tested the same request on postman without the query string.. and surely I got the correct html back:

解决方案

Try renaming files in your view from something.erb to something.html.erb and something.js.erb. When you write
respond_to :js, :html, :json
...
respond_with self.resource Controller first tries to render something.js.erb then something.html.erb etc. Without extension it doesn't know what view to render.

这篇关于Rails 控制器何时呈现为 JS、HTML 或“q"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆