【问题标题】:Rails render js file but can't execute itRails渲染js文件但无法执行
【发布时间】:2015-10-29 10:32:20
【问题描述】:

我在 Rails (3.2.6) 应用程序上的 Ruby (1.9.3p194 (2012-04-20 修订版 35410) [x86_64-linux]) 有问题:我需要执行一个 js 文件,但我只能打印出来。

查看:

291             <%= form_for :change_top_ten, remote: true, url: {controller: :users, action: :change_top_ten, format: :js} do |f| %>
292                 <%= f.select :status, options_for_select(@categories_and_interests, selected: "tutti"), {class: :'deafult-select'}, onchange: "this.form.submit();" %>
293             <% end %>

控制器:

1658   def change_top_ten
1659     @filter = params[:change_top_ten][:status]
1660   end

change_top_ten.js.erb

  1 alert('here');

服务器日志:

Started POST "/change_top_ten.js" for 10.0.2.2 at 2015-10-29 10:30:10 +0000
Processing by UsersController#change_top_ten as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"+v0oW+p0fy9IxpfbaKTj7g9yZSzffK+SQG52Mx3vjwQ=", "change_top_ten"=>{"status"=>"prof_2"}}
  User Load (56.4ms)  SELECT "users".* FROM "users" WHERE "users"."has_confirmed" = 't' AND "users"."id" = ? LIMIT 1  [["id", 13]]
  Rendered users/change_top_ten.js.erb (0.1ms)
Completed 200 OK in 154.7ms (Views: 34.9ms | ActiveRecord: 66.0ms)

文件呈现为 html 页面:空白页面,带有“alert('here');”印在上面。

编辑: 我正在编辑问题以回复第一条评论:

应用程序.js

  1 // This is a manifest file that'll be compiled into application.js, which will include all the files
  2 // listed below.
  3 //
  4 // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
  5 // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
  6 //
  7 // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
  8 // the compiled file.
  9 //
 10 // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
 11 // GO AFTER THE REQUIRES BELOW.
 12 //
 13 //# require angular
 14 //= require jquery
 15 //= require jquery_ujs
 16 //= require jquery.countdown
 17 //= require jquery.remotipart
 18 //# require_tree .

导入 Jquery 是因为我有其他调用,例如此处描述的调用,它们运行良好

【问题讨论】:

  • 你没有错过 rails-jquery javascripts 吗?即//= require jquery_ujs 中的application.js 行?
  • 我想你的问题在这里得到了回答rails form submission with remote => true
  • 请检查服务器日志中的请求content-type。我确定它会是application/text

标签: javascript ruby-on-rails ruby


【解决方案1】:

好的,因此对您的 ajax 查询的响应不会被评估为 javascript。 尝试将以下内容添加到您的 application.js 文件或在呈现表单之前将执行它的位置。

$.ajax({
    url: this.href,
    dataType: "script",
    beforeSend: function(xhr) {xhr.setRequestHeader("Accept", text/javascript");}
});

这是说将所有 ajax 调用的数据类型设置为“脚本”,这应该意味着响应被评估为 javascript。

【讨论】:

  • 嗨 Paul D 是 text/javscript 后的双引号符号正确吗?
  • 我添加了你的代码(文本/javascirpt 用双引号括起来)并且没有任何改变:(
【解决方案2】:

您只需将 respond_to js 放入您的方法中。请尝试以下代码:

def change_top_ten
  @filter = params[:change_top_ten][:status]
  respond_to do |format|
    format.js
  end
end

它将运行您的change_to_ten.js.erb 文件。希望对您有所帮助。

【讨论】:

  • 相同结果:在 2015-10-29 15:58:22 +0000 由 UsersController#change_top_ten 作为 JS 参数处理 10.0.2.2 开始 POST "/change_top_ten.js":{"utf8"= >"✓", "authenticity_token"=>"+v0oW+p0fy9IxpfbaKTj7g9yZSzffK+SQG52Mx3vjwQ=", "change_top_ten"=>{"status"=>"prof_1"}} 用户负载 (2.0ms) SELECT "users".* FROM " users” WHERE “users”.“has_confirmed” = 't' AND “users”.“id” = ? LIMIT 1 [["id", 13]] Rendered users/change_top_ten.js.erb (0.4ms) Completed 200 OK in 150.5ms (Views: 26.0ms | ActiveRecord: 71.6ms)
【解决方案3】:

我找到了一个临时解决方案。问题是这段代码:

onchange: "this.form.submit();"

删除后添加此代码:

<%= f.submit :Save, class: :'btn', type: :submit %>

js文件按原样执行。

如何使用 onchange 方法提交表单并执行我的 js 代码?

【讨论】:

    【解决方案4】:

    好的,所以您想在选择更改时提交表单,这与您的原始问题有点不同。

    您想在 application.js 文件中添加一些 javascript,该文件绑定到选择控件的 onChange 事件。

    即像这样:

    $('select').onChange(function()
      {
        $('form').submit();
      }
    );
    

    当任何选择发生变化时,它应该提交页面上的任何表单。 您可能希望将绑定范围限定为特定的选择并提交到特定的表单。

    【讨论】:

      【解决方案5】:

      确保它具有打开的 javascript 标记,例如

      <script type="text/javascript">
          enter code here
      </script>
      

      【讨论】:

        猜你喜欢
        • 2022-01-08
        • 2021-11-02
        • 2016-04-29
        • 1970-01-01
        • 1970-01-01
        • 2012-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多