最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Rails 3.1: The new way to use 'respond_to ... format.js'without the 'page' variable

programmeradmin0浏览0评论

I am trying to update Ruby on Rails to the version 3.1. I followed the Upgrading to Rails 3.1 screencast and all seems to work except for statements as

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:

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:

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

BTW : ...but, is it recommended to use JavaScript code directly in a controller file?

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:

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"? How RoR 3.1 would handle these cases?

I am trying to update Ruby on Rails to the version 3.1. I followed the Upgrading to Rails 3.1 screencast and all seems to work except for statements as

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:

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:

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

BTW : ...but, is it recommended to use JavaScript code directly in a controller file?

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:

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"? How RoR 3.1 would handle these cases?

Share Improve this question edited Sep 15, 2011 at 23:20 Backo asked Sep 15, 2011 at 21:35 BackoBacko 18.9k28 gold badges107 silver badges177 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

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:

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.

Instead or .rjs, you can use .js.erb files. They act in a similar manner, allowing access to instance variables and helper methods.

发布评论

评论列表(0)

  1. 暂无评论