Rails 3:如何从控制器发送 Javascript 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5472227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:18:57  来源:igfitidea点击:

Rails 3: How to send Javascript code from Controller?

javascriptruby-on-railsruby-on-rails-3

提问by Misha Moroshko

When foomethod of MyControlleris called via Ajax, it may return Javascript code like this:

当通过 Ajax 调用fooof 的方法时MyController,它可能会返回这样的 Javascript 代码:

class MyController < ApplicationController
  def foo
    render :js => "alert('Hello');"
  end
end

Is that possible to do something similar to return a Javascript code when foois called normally (not via Ajax) ? I would to do something like this:

foo正常调用时(不是通过 Ajax),是否可以执行类似于返回 Javascript 代码的操作?我会做这样的事情:

class Job < ApplicationController
  def edit
    if user_type == 'demo'
      [Here I would like to display a Javascript alert saying that 
       the job cannot be edited in demo mode. How would you do this?]
    else
      @job = Job.find(params[:id])
    end
  end
end

采纳答案by Jean

short answer is : You can't.

简短的回答是:你不能。

When you render for :js, the calling code is a javascript framework which is aware that it requested js and will execute the returned code to make it take effect when the asychronous call executes it's onSuccess action.

当你为 :js 渲染时,调用代码是一个 javascript 框架,它知道它请求了 js 并将执行返回的代码以使其在异步调用执行时生效,它是 onSuccess 操作。

When rendering for the default, the calling code is the browser which expects html it won't execute pure js.

默认渲染时,调用代码是浏览器,它期望 html 它不会执行纯 js。

What you can do is make your view return html with some javascript at the beginning with a window.onload() handler defined to make it display the alert. But you still have to return the html (if you don't want to display the data, make it redirect to the previous view template with a specific argument so the view will have the event handler) (https://stackoverflow.com/questions/1033398/execute-javascript-when-page-has-fully-loaded)

您可以做的是让您的视图在开始时使用一些 javascript 返回 html,并定义一个 window.onload() 处理程序以使其显示警报。但是您仍然必须返回 html(如果您不想显示数据,请使用特定参数将其重定向到上一个视图模板,以便视图具有事件处理程序)(https://stackoverflow.com/问题/1033398/execute-javascript-when-page-has-fully-loaded)

You could also change the event handler which calls the edit action to display the alert even before the action is executed. to prevent a user from disabling js and getting over the alert, you need to still make the check on user_type and maybe simply redirect to the previous page.

您还可以更改调用编辑操作的事件处理程序,以在执行操作之前显示警报。为了防止用户禁用 js 并跳过警报,您仍然需要检查 user_type 并且可能只是重定向到上一页。

回答by Jeffrey W.

I recommend that you keep your controller clean and place any JavaScript code in a rjs template:

我建议您保持控制器干净,并将所有 JavaScript 代码放在 rjs 模板中:

Controller

控制器

class MyController < ApplicationController
   def edit
      render do |page|
        page.html {}
        page.js {}
      end
   end
 end

edit.js.erb

编辑.js.erb

alert("hello");

Whenever /my/edit/1.jsis called (via: edit_my_path(1, :format => :js)) the edit.js.erb would be rendered displaying a alert.

每当/my/edit/1.js被调用(通过:edit_my_path(1, :format => :js))edit.js.erb 将呈现显示警报。

Whenever /my/edit/1.jsis called (via: edit_my_path(1)) the edit.html.erb would be rendered displaying normal html.

每当/my/edit/1.js被调用(通过:edit_my_path(1))edit.html.erb 将呈现显示正常的 html。

If you would need to check whether the request is ajax (and thus not just a normal .js call) you could, in your controller do: request.xhr?

如果您需要检查请求是否是 ajax(因此不仅仅是普通的 .js 调用),您可以在控制器中执行以下操作: request.xhr?

Hope that helps!

希望有帮助!

回答by David

You can simply render the javascript as text when the js format is requested

当请求 js 格式时,您可以简单地将 javascript 呈现为文本

class MyController < ApplicationController
   def update
      respond_to do |format|
        format.html {}
        format.js { render text: 'alert();' }
      end
   end
 end