Ruby on rails 如何在控制器中禁止除html以外的任何响应
我有这样的代码:Ruby on rails 如何在控制器中禁止除html以外的任何响应,ruby-on-rails,Ruby On Rails,我有这样的代码: def show @attachment = Attachment.find(params[:id]) respond_to do |format| format.html end end 此代码允许对json进行响应,但不呈现任何内容。如何在除html之外的任何其他请求上显示自定义页面 您可以使用格式。任何用于除html以外的任何其他请求: def show @attachment = Attachment.find(params[:id])
def show
@attachment = Attachment.find(params[:id])
respond_to do |format|
format.html
end
end
此代码允许对json进行响应,但不呈现任何内容。如何在除html之外的任何其他请求上显示自定义页面 您可以使用
格式。任何
用于除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
您可以使用
格式。任何
用于除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
尝试:
尝试:
在config/routes.rb中定义路由时,可以限制路由的格式
scope :format => true, :constraints => {:format => :html} do
resources :attachments
end
定义要在该范围内限制其格式的所有路由。在config/routes.rb中定义路由时,可以限制路由的格式
scope :format => true, :constraints => {:format => :html} do
resources :attachments
end
定义要限制其格式在该范围内的所有路由。谢谢。还有更普遍的解决办法吗?写这行太乏味了,谢谢。还有更普遍的解决办法吗?写这行太乏味了