简体   繁体   English

Rails 3:如何从Controller发送Javascript代码?

[英]Rails 3: How to send Javascript code from Controller?

When foo method of MyController is called via Ajax, it may return Javascript code like this: 当通过Ajax调用MyController foo方法时,它可能返回如下的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 foo is called normally (not via Ajax) ? 当正常调用foo (而不是通过Ajax)时,是否可以执行类似的操作以返回Javascript代码? I would to do something like this: 我会做这样的事情:

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

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. 你可以做的是让你的视图在开头用一些javascript返回html,并定义一个window.onload()处理程序,使其显示警告。 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 ) 但是你仍然需要返回html(如果你不想显示数据,让它重定向到带有特定参数的前一个视图模板,这样视图就会有事件处理程序)( https://stackoverflow.com/ questions / 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,并且可能只是重定向到上一页。

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 edit.js.erb

alert("hello");

Whenever /my/edit/1.js is 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.js is 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! 希望有所帮助!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM