Ruby-on-rails Rails:使用控制器渲染 js.erb 模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9735866/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:03:22  来源:igfitidea点击:

Rails: rendering the js.erb template using the controller

ruby-on-railsajax

提问by chourobin

I have a rails app trying to incorporate some AJAX where clicking new opens a modal window and a form. I want to be able to display the validation errors if it fails so in my create action, i thought about re-rendering the new.js.erb file. Is this the right approach?

我有一个 rails 应用程序试图合并一些 AJAX,其中单击新打开一个模式窗口和一个表单。如果验证错误失败,我希望能够显示验证错误,因此在我的创建操作中,我考虑重新呈现 new.js.erb 文件。这是正确的方法吗?

def create
    @place = Place.new(params[:place])
    if @place.save
       redirect_to places_path, :notice => "Successfully created place"
    else
       render "new.js.erb"
    end
end

The result I get is escaped js text in my browser like:

我得到的结果是浏览器中转义的 js 文本,例如:

$("#new_grouping").html("<div class=\"modal-header\">\n  <a class=\"close\" data-   dismiss=\"modal\">×<\/a>\n  <h3>Create a new menu section<\/h3>\n<\/div>\n<form accept-charset=\"UTF-8\" action=\"/places/1-mama-s-pizza/groupings\" class=\"simple_form new_grouping\" id=\"new_grouping\" method=\"post\" novalidate=\"novalidate\">

I've tried putting various options into the render block but no luck. Any tips?

我试过将各种选项放入渲染块,但没有运气。有小费吗?

回答by Vapire

The best practice would be to support both, AJAX and Non-AJAX calls, in case the user has javascript turned off for any reason.

最佳实践是同时支持 AJAX 和非 AJAX 调用,以防用户因任何原因关闭 javascript。

def create
  @place = Place.new(params[:place])

  respond_to do |format|
    if @place.save
      format.html { redirect_to places_path, :notice => "Successfully created place" }
      format.js   # renders create.js.erb, which could be used to redirect via javascript
    else
      format.html { render :action => 'new' }
      format.js { render :action => 'new' }
    end
  end
end

The render :action => 'new'actually renders the template of the controller action newwhich results to new.html.erbrespectively to new.js.erbdepending if it's a non-AJAX or an AJAX call.

render :action => 'new'实际呈现控制器动作的模板new,其结果new.html.erb分别new.js.erb取决于它是否是一个非AJAX或AJAX调用。

In new.js.erbgoes your ERB/javascript code:

new.js.erb去您的ERB / javascript代码:

$("#new_grouping").html("<%= escape_javascript(...) %>">

回答by ibylich

As i know, rendering partial in controller is a bad idea, because then response can be without content-type and some browsers can't understand this. if it is some file attached to action you should write

据我所知,在控制器中渲染部分是一个坏主意,因为响应可能没有内容类型,并且某些浏览器无法理解这一点。如果它是附加到操作的某个文件,您应该编写

render :action => "create"

or if you need just render a singe partial then in your action file write

或者如果你只需要渲染一个部分然后在你的动作文件中写

<%= render :partial => "path/to/partial" %>

as i said, then you won't have problems with content-type in response

正如我所说的那样,您将不会遇到 content-type 作为响应的问题