Ruby on rails 如何防止向用户显示json数据?
此url返回Ruby on rails 如何防止向用户显示json数据?,ruby-on-rails,caching,http-caching,Ruby On Rails,Caching,Http Caching,此url返回json https://example.com/product/product_name.json // returns json document 及 返回html,该html使用jquery在内部调用上述JSON url,它将对其进行构造并很好地呈现数据 在控制器中,我正在进行缓存 def product expires_in 1.day , public: true render json: @product end 现在的问题是,如果用户直接访问https://
json
https://example.com/product/product_name.json // returns json document
及
返回html,该html使用jquery在内部调用上述JSON url,它将对其进行构造并很好地呈现数据
在控制器中,我正在进行缓存
def product
expires_in 1.day , public: true
render json: @product
end
现在的问题是,如果用户直接访问https://example.com/product/product_name
一开始,它加载html
,但当他再次访问时,他看到的是json
数据
是否有任何方法可以防止或控制这种情况,以便用户不会看到json
PS:http
缓存最适合我们,我知道其他缓存技术
更新基于格式的渲染不起作用。我猜这是因为请求没有命中服务器,因为http\u缓存
。无论如何,这就是我在应用程序\u控制器中所做的
before_filter :intercept_html_requests, :unless => :not_format_html?
private
def not_format_html?
request.format != Mime::HTML
end
def intercept_html_requests
render('homepage/index')
end
为什么不基于请求的格式进行渲染?@apreading查看更新的问题。我认为基于请求的格式进行渲染应该可以。你能告诉我们你的路线吗?当你尝试的时候,你在你的控制器中写了什么?上面的代码是最小的代码,实际的代码太大了。这个路径看起来像是match”/product/:product_url“=>“products#product”
th
before_filter :intercept_html_requests, :unless => :not_format_html?
private
def not_format_html?
request.format != Mime::HTML
end
def intercept_html_requests
render('homepage/index')
end