2015-10-29 28 views
0

在Rails(3.2.6)应用程序中,我的Ruby(1.9.3p194(2012-04-20 revision 35410)[x86_64-linux])出现问题:我需要执行一个js文件,但我只能打印它。Rails渲染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')”的空白页面;“印在上面。

编辑: 我编辑的问题回复的第一个注释:

的application.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是进口的,因为我有其他电话像这里描述的和thery被工作正常

+0

你不缺少轨-的jQuery JavaScript的?即'// =在'application.js'中需要jquery_ujs'行? –

+0

我想你的问题在这里得到解答 [rails form submission with remote => true](http://stackoverflow.com/questions/20628013/rails-form-submission-with-remote-true-js-file-renders-但是不执行) –

+0

请检查服务器日志中的请求'content-type'。我确定它会是'application/text' –

回答

0

你只是把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文件。我希望这对你有所帮助。

+0

同样的结果: 10.0.2.2 2015.10-29 15:58:22 +0000 开始POST“/change_top_ten.js”处理UsersController#change_top_ten as JS 参数:{“utf8”=>“✓”,“authenticity_token”=>“+ v0oW + p0fy9IxpfbaKTj7g9yZSzffK + SQG52Mx3vjwQ =”,“change_top_ten”=> {“status”=>“prof_1”}} User Load(2.0 ms)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) 在150.5ms内完成200 OK(查看:26.0ms | ActiveRecord:71.6ms) – ste

0

好,所以你想提交表单时,选择被改变,这是有点不同于你原来的问题。

你想添加一些javascript到你的application.js文件,它绑定到选择控件的onChange事件。

即是这样的:

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

任何时候选择改变这应该提交页面上的任何形式。 您可能希望将绑定范围限定为特定的选择并将其提交到特定的表单。

+0

这不工作:它只是打印警告由脚本标记sourrounded ... – ste

0

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

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

这是做什么说是设置所有ajax调用的数据类型为'脚本'这应该意味着响应被评估为JavaScript。

+0

嗨保罗D是文本/ javscript正确后的双引号符号? – ste

+0

我添加了你的代码(带有双引号的文本/ javascirpt),没有任何改变:( – ste

0

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

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

删除它的添加该代码后:

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

JS文件是像它应该执行。

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

0

确保它已打开JavaScript代码,前

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