Rails - Controller Action can only be executed by JS call
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
精彩评论