开发者

Using render(:action => "action") in a js.erb file

I try to assign to result of rendering an action to an element on page. That should be an easy task but the following statement:

$(".box:first").after("<%= escape_jav开发者_JAVA技巧ascript(render :action => "new" ) %>");

will result in the following error message:

ActionView::Template::Error (undefined method `formats' for nil:NilClass):
    1: $(".box:first").after("<%= escape_javascript(render(:action => "new") ) %>");
  • The same code works if i try to render a partial using :partial => "new" (given that the partial with the given name exists)
  • The error message will be the same if I change ':action' f.e. to ':foobar'.

Any ideas what i am doing wrong or is this simple a bug in rails? (3.0.7)


It is a bug in your application. Actually it is a bug in your new action in controller


Result of rendering of an action? That's probably not the very best idea. As you said, the code works when you use partial, and that's what you should use. This is from official Rails guides:

Using render with :action is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does not run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling render.

http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜