开发者

Rails - Controller Action can only be executed by JS call

开发者 https://www.devze.com 2023-02-10 02:23 出处:网络
In my view file, I use a collection_select with an :onchange => remote_function that execute another action from the same controller (the action will eventually update the page content)

In my view file, I use a collection_select with an :onchange => remote_function that execute another action from the same controller (the action will eventually update the page content)

I'd like this action to be only accessible when called by the JS but not when the corresponding route is directly entered as an URL in the browser.

Any idea how I could开发者_如何学JAVA do this ?


You can use request.xhr? to check the request type, either its AJAX request or others (post, get). It returns true or false. If true you can perform the action.


You could use respond_with and respond_to

class MyController
  respond_to :js

  def index
    respond_with(@users = User.all)
  end
end
0

精彩评论

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