简体   繁体   English

Rails 3.1:使用'respond_to ... format.js'而不使用'page'变量的新方法

[英]Rails 3.1: The new way to use 'respond_to … format.js' without the 'page' variable

I am trying to update Ruby on Rails to the version 3.1 . 我试图将Ruby on Rails更新到3.1版。 I followed the Upgrading to Rails 3.1 screencast and all seems to work except for statements as 我跟着升级到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. 我知道问题与RJS模板有关,因为它们在3.1版本中不再可用。 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: 因此,为了运行一些JavaScript代码,在我的控制器文件中,我可能\\应该\\可以插入一些JavaScript代码,如下例所示:

format.js { render :js => "alert('Hello Rails');" }

BTW : ... but , is it recommended to use JavaScript code directly in a controller file? BTW :... 但是是否建议直接在控制器文件中使用JavaScript代码?

Considering the above code in the create controller action, I would like to redirect user to the @article path after a successful submission. 考虑到create控制器操作中的上述代码,我想在成功提交后将用户重定向到@article路径。 I can do: 我可以:

format.js { render :js => "window.location.replace('#{article_url(@article)}');" }

..but, how can I do the same thing by following the "Ruby on Rails Way to do things"? ..但是, 我怎么能按照 “Ruby on Rails方式做事”来做同样的事情? How RoR 3.1 would handle these cases? RoR 3.1如何处理这些案件?

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 :) 注意我刚刚做了 - 没测试:)

Using javascript code directly in controller files is not recommended. 建议不要直接在控制器文件中使用javascript代码。

Instead or .rjs , you can use .js.erb files. 相反,或.rjs ,您可以使用.js.erb文件。 They act in a similar manner, allowing access to instance variables and helper methods. 它们以类似的方式运行,允许访问实例变量和辅助方法。

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

相关问题 respond_to 做 |格式| format.js 在 Rails 6.1.3 中不起作用 - respond_to do |format| format.js not working in rails 6.1.3 使用response_to format.js替换Rails上textarea的内容 - using respond_to format.js to replace the content of a textarea on rails Rails:make(link_to“示例”,“#”,远程:true)绕过(respond_to…format.js) - Rails: make (link_to “example”, “#”, remote: true) bypass (respond_to … format.js) 如果存在format.html,则response_to无法与format.js一起使用 - respond_to not working with format.js if format.html is present response_to块中的Format.js无法以条件格式运行 - Format.js in respond_to block doesn't work in an conditional format 使用response_来做| format | 在将照片收藏在模态内之后,format.js在模态内显示消息? - using respond_to do |format| format.js to show a message inside of a modal after photo is favorited inside a modal? response_to js“未知格式” - respond_to js “unknown format” 如何在控制器上的 format.js 之后使用实例变量或参数 - Rails - How to use instance-variable or param after a format.js on controller - Rails Rails:format.js 将我重定向到空白页面 - Rails: format.js redirecting me to a blank page Rails 4 ActionController :: UnknownFormat format.js错误 - Rails 4 ActionController::UnknownFormat format.js error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM