Ruby-on-rails 如何在 Rails 的 response_with 中指定“:layout => false”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3876891/
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
How do I specify ":layout => false" in Rails' respond_with?
提问by Lance Pollard
I have this setup:
我有这个设置:
class UsersController < InheritedResources::Base
respond_to :html, :js, :xml, :json
def index
@users = User.all
respond_with(@users)
end
end
Now I am trying to make it so, if params[:format] =~ /(js|json)/, render :layout => false, :text => @users.to_json. How do I do that with respond_withor respond_toand inherited_resources?
现在我正在努力做到这一点,如果params[:format] =~ /(js|json)/, render :layout => false, :text => @users.to_json. 我如何使用respond_withorrespond_to和inherited_resources做到这一点?
回答by Yannis
Something like:
就像是:
def index
@users = User.all
respond_with @users do |format|
format.json { render :layout => false, :text => @users.to_json }
end
end
回答by Anthony Bishopric
Assuming you need JSON for an Ajax request
假设您需要 JSON 来处理 Ajax 请求
class UsersController < InheritedResources::Base
respond_to :html, :js, :xml, :json
def index
@users = User.all
respond_with(@users, :layout => !request.xhr? )
end
end
This seems like the cleanest solution to me.
这对我来说似乎是最干净的解决方案。
回答by Jeremy
Or to prevent you having to hardcode responses for each format in each action.
或者防止您必须对每个操作中的每种格式的响应进行硬编码。
If you have no layouts for any of the actions in this controller it would be nicer to do:
如果此控制器中的任何操作都没有布局,则最好这样做:
class UsersController < InheritedResources::Base
respond_to :html, :js, :xml, :json
layout false
def index
@users = User.all
respond_with(@users)
end
end
回答by choonkeat
I love @anthony's solution, but didn't work for me... I had to do:
我喜欢@anthony 的解决方案,但对我不起作用……我必须这样做:
respond_with(@users) do |format|
format.html { render :layout => !request.xhr? }
end
ps: posting an "answer" instead of a comment because stackoverflow comment formatting and "return key == submit" is infuriating!
ps:发布“答案”而不是评论,因为stackoverflow评论格式和“返回键==提交”令人气愤!
回答by Jonathan Allard
I just found this out:
我刚刚发现了这一点:
Even if it's JSON, Rails is still looking for a layout. As such, the only layout that it finds, in our case, is application.html.
即使是 JSON,Rails 仍然在寻找布局。因此,在我们的例子中,它找到的唯一布局是application.html.
Solution: Make a JSON layout.
解决方案:制作一个 JSON 布局。
So for instance, if you put an empty application.json.erbwith a single = yieldinside, next to your HTML one, the HTML layout is bettered by the JSON one. You can even use this to surround your JSON with metadata or things like that.
因此,例如,如果您在 HTML 旁边放一个空的application.json.erb,= yield里面有一个,则 HTML 布局优于 JSON 布局。你甚至可以用它来用元数据或类似的东西包围你的 JSON。
<%# app/views/layouts/application.json.erb %>
<%= yield %>
No other parameters needed, it automagically works!
不需要其他参数,它自动工作!
Tested in Rails 4 only
仅在 Rails 4 中测试
回答by Moriarty
class UsersController < InheritedResources::Base
layout -> (controller) { controller.request.xhr? ? false : 'application' }
end
回答by rafudu
You need to set this on your show action.
你需要在你的表演动作上设置它。
def show
render :layout => !request.xhr?
end
:)
:)

