如何获得在 Rails 3 中渲染 javascript 的方法?
我正在尝试设计在出现错误时使用特定的错误模板。它正在访问正确的页面,但它正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将上面的代码段替换为:
如果您想为其创建单独的模板,可以将其保存在
/errors/***.html
中,然后进行渲染。或者,您可以使用 JavaScript 在客户端处理错误。
Replace the above code segment with:
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.
所以,我想,迟到总比不到好(但希望这能帮助任何通过谷歌偶然发现这个页面的人)。
为了让 Devise 正确渲染 javascript,我必须做的就是告诉它不要渲染任何类型的布局,唉:
这成功了,现在我的 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:
That did the trick, and now my JS fires as expected.