开发者

Redirect user from ajax routes

For instance, I have generated by resources :companies routes:

/company/123/
/company/edit/123
/company/new

All these routes are used for Ajax functionality.

And now if user opens it directly in browser, he/she will see all raw JSON data.

How to redirect these requests like theese to the site root (http://site.com/)?

I think, we can use x-requested-with HTTP header to detect ajax开发者_如何学JAVA/direct requests, but I don't know how to deal with it in Rails.


  before_filter :check_ajax

  def check_ajax
    if (request.env['HTTP_X_REQUESTED_WITH'] !~ /XMLHttpRequest/i)
      redirect_to '/nahuy/' # your url
    end
  end

Redirects all non-Ajax requests '/nahuy/' :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜