How to do an almost-postback in rails
I don't know what to call this, except that it is almost-a-postback. I'd like my rails app to be able to receive a request independently from somewhere else on the interweb in the form of:
myapp.com/count?id=45&sex=y
开发者_JS百科
And to be able to perform some calculations (update some table etc) and not bother to respond back.
The key here is - I just want to perform the action and nothing else, not display any view or redirect to any other page.
I set up a "count" controller, but it wants some view or web page to go to. Then I thought, maybe routes could do it? But that seems like a bad idea to have code in routes.
Any ideas appreciated.
In your method controller do this:
def method_count
#do calculation stuff
render :nothing => true
end
This will return status 200 (if everything was ok) and nothing will be rendered.
Don't forget to setup your route in config/routes.rb
For API calls it is a good idea to return only an HTTP status code if no content has been requested. You can do this using the head
method in ActionController::Base.
def count
# do work
head :success
end
You have to create a controller and put your action into it !
In config/routes.rb :
get "yourcontroller/count"
In yourcontroller.rb :
def count
#some code treating params
end
精彩评论