如何获得在 Rails 3 中渲染 javascript 的方法?

发布于 2024-10-26 12:46:57 字数 973 浏览 1 评论 0原文

我正在尝试设计在出现错误时使用特定的错误模板。它正在访问正确的页面,但它正在尝试将 javascript 呈现为布局本身。

这是我的 application.rb layout_by_resource 代码:

def layout_by_resource
  if devise_controller? && !current_user.nil? && !current_user.role.nil? && (current_user.role == "superadmin" || current_user.role == "admin")
    "admin"
  elsif devise_controller? && !current_user.nil? && !current_user.role.nil? && current_user.role == "user"
    "user"
  elsif devise_controller? && current_user.nil?
    respond_to do |format|
      format.js { render :action => "layouts/errors", :layout => false, :locals => { :current_object => resource } }
      format.html { "application" }
    end
  else
    "application"
  end
end

这是我的设计表单代码:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :remote => true) do |f| %>

<% end %>

I am trying to get devise to use a specific error template when there are errors. It is getting to the correct page, but it is trying to render the javascript as the layout itself.

Here is my application.rb layout_by_resource code:

def layout_by_resource
  if devise_controller? && !current_user.nil? && !current_user.role.nil? && (current_user.role == "superadmin" || current_user.role == "admin")
    "admin"
  elsif devise_controller? && !current_user.nil? && !current_user.role.nil? && current_user.role == "user"
    "user"
  elsif devise_controller? && current_user.nil?
    respond_to do |format|
      format.js { render :action => "layouts/errors", :layout => false, :locals => { :current_object => resource } }
      format.html { "application" }
    end
  else
    "application"
  end
end

Here is my devise form code:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :remote => true) do |f| %>

<% end %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭⌒浅淡时光〆 2024-11-02 12:46:58
respond_to do |format|
      format.js { render :action => "layouts/errors", :layout => false, :locals => { :current_object => resource } }
      format.html { "application" }
end

将上面的代码段替换为:

respond_to do |format|
      # Show a neat html page inside the app's layout for web users
      format.html { render :template => "errors/#{status}", :status => status }

      # Everything else (JSON, XML, YAML, Whatnot) gets a blank page with status
      # which can then be understood and processed by the API client,
      # JavaScript library (on Ajax) etc.
      format.all { render :nothing => true, :status => status }
    end

如果您想为其创建单独的模板,可以将其保存在 /errors/***.html 中,然后进行渲染。

或者,您可以使用 JavaScript 在客户端处理错误。

respond_to do |format|
      format.js { render :action => "layouts/errors", :layout => false, :locals => { :current_object => resource } }
      format.html { "application" }
end

Replace the above code segment with:

respond_to do |format|
      # Show a neat html page inside the app's layout for web users
      format.html { render :template => "errors/#{status}", :status => status }

      # Everything else (JSON, XML, YAML, Whatnot) gets a blank page with status
      # which can then be understood and processed by the API client,
      # JavaScript library (on Ajax) etc.
      format.all { render :nothing => true, :status => status }
    end

If you want to create a separate template for it, you can do so and save it in /errors/***.html and then render it.

OR, you could handle the error on the client side using javascript.

梦断已成空 2024-11-02 12:46:58

所以,我想,迟到总比不到好(但希望这能帮助任何通过谷歌偶然发现这个页面的人)。

为了让 Devise 正确渲染 javascript,我必须做的就是告诉它不要渲染任何类型的布局,唉:

render "fail.js", :layout => false

这成功了,现在我的 JS 按预期触发。

So, better late than never, I guess (but hopefully this will help anyone who stumbles upon this page via the Googles).

What I had to do to get Devise to properly render javascript was tell it not to render any sort of layout, ala:

render "fail.js", :layout => false

That did the trick, and now my JS fires as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文