在Rails中使用respond_to
而不是case語句有什麼好處? 我有幾個實例變量,我想爲某些格式設置相同的方式,但不適用於HTML。這似乎並不工作:Rails:respond_to vs「case」conditional
respond_to do |format|
format.html do
# ...
end
format.any(:csv, :xml) do
# common stuff
end
format.csv do
# ...
end
format.xml do
# ...
end
end
我想我會最終使用了幾個case request.format
,而不是使用respond_to
都:
case request.format
when 'html'
# ...
when 'csv', 'xml'
# common stuff
end
# more common stuff
case request.format
when 'html'
# render
when 'csv'
# custom render csv
when 'xml'
# render xml with a template
end
所以我不知道什麼是很好用案例爲respond_to
,其中case request.format
看起來不會更好?
我很懶,現在就試試,你的意思是'respond_to do | f | f.html; f.json end'也會告訴Rails在你的例子中以406錯誤迴應? (在你的例子中,它是'respond_to:html,:json') – Alexey 2013-09-15 13:02:32
是的,所以respond_to需要** ** block或者'mime types'數組 – j03w 2013-09-15 23:39:16