javascript Rails 3.1:在没有“page”变量的情况下使用“respond_to ... format.js”的新方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7437717/
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
Rails 3.1: The new way to use 'respond_to ... format.js' without the 'page' variable
提问by Backo
I am trying to update Ruby on Rails to the version 3.1. I followed the Upgrading to Rails 3.1screencast and all seems to work except for statements as
我正在尝试将 Ruby on Rails 更新到版本3.1。我按照升级到 Rails 3.1截屏视频进行操作,除了作为
format.js { render(:update) { |page| page.redirect_to @article } }
In many controllers I have code like the following:
在许多控制器中,我有如下代码:
def create
...
respond_to do |format|
format.js { render(:update) { |page| page.redirect_to @article } }
end
end
In all above cases, when I try to submit related forms performing JS requests, I get the following error:
在上述所有情况下,当我尝试提交执行 JS 请求的相关表单时,出现以下错误:
ActionView::MissingTemplate (Missing template articles/update, application/update with {:handlers=>[:erb, :builder, :coffee], :formats=>[:js, :html], :locale=>[:en, :en]}. Searched in:
* "/<MY_PROJECT_PATH>/app/views"
):
app/controllers/articles_controller.rb:114:in `create'
Rendered /<...>/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.3ms)
I know that the problem was related to RJS templates because them are not more available in the 3.1 release. So, in order to run some JavaScript code, in my controller files I may\should\can insert some JavaScript code as made in the following example:
我知道问题与 RJS 模板有关,因为它们在 3.1 版本中不再可用。因此,为了运行一些 JavaScript 代码,我可能\应该\可以在我的控制器文件中插入一些 JavaScript 代码,如下例所示:
format.js { render :js => "alert('Hello Rails');" }
BTW: ...but, is it recommended to use JavaScript code directly in a controller file?
顺便说一句:...但是,是否建议直接在控制器文件中使用 JavaScript 代码?
Considering the above code in the create
controller action, I would like to redirect user to the @article
path after a successful submission. I can do:
考虑到create
控制器动作中的上述代码,我想@article
在成功提交后将用户重定向到路径。我可以:
format.js { render :js => "window.location.replace('#{article_url(@article)}');" }
..but, how can I do the same thing by followingthe "Ruby on Rails Way to do things"? How RoR 3.1 would handle these cases?
..但是,我怎样才能通过遵循“Ruby on Rails 做事方式”来做同样的事情?RoR 3.1 将如何处理这些情况?
回答by Mike
I'd go with:
我会去:
render js: %(window.location.href='#{article_path @article}')
Or put something along the lines of this in your application_controller.rb
:
或者在你的application_controller.rb
:
def js_redirect_to(path)
render js: %(window.location.href='#{path}') and return
end
Then in your controllers you can just call:
然后在你的控制器中你可以调用:
js_redirect_to(article_path @article)
Note I just made this up - not tested :)
请注意,我只是编造了这个 - 没有经过测试:)
回答by Alex
Using javascript code directly in controller files is not recommended.
不建议直接在控制器文件中使用 javascript 代码。
Instead or .rjs
, you can use .js.erb
files. They act in a similar manner, allowing access to instance variables and helper methods.
或者.rjs
,您可以使用.js.erb
文件。它们以类似的方式运行,允许访问实例变量和辅助方法。