我有这样的代码:
def show
@attachment = Attachment.find(params[:id])
respond_to do |format|
format.html
end
end
此代码允许响应json,但不呈现任何内容。如何在除html之外的任何其他请求上显示自定义页面?
答案 0 :(得分:2)
您可以将format.any
用于除html以外的任何其他请求:
def show
@attachment = Attachment.find(params[:id])
respond_to do |format|
format.html { render text: => 'This is html' }
format.any { render :text => "Only html is supported" }
end
end
答案 1 :(得分:2)
尝试:
before_filter do
render text: 'Wrong type', status: 406 unless request.format == Mime::HTML
end
答案 2 :(得分:2)
在config / routes.rb
中定义路由时,可以限制路由的格式scope :format => true, :constraints => {:format => :html} do
resources :attachments
end
定义要在该范围内限制其格式的所有路由。