I have a webservice that processes user submitted data. The user doesn't need to receive a response based on that data, he just needs to submit it. However, I do need to process that data. Currently the processing takes place directly as a response to the post action, and after the processing, it return a statuscode. This normally takes 0.5s - 2s, but sometimes much longer, when a user submits a lot of data.
Is it possible in Rails to spawn a new thread for processing that data, while returning the status code (and thus finishing the request for the user)?
When writing controllers in Ruby on rails, using before_action (used to be called before_filter in earlier versions) is your bread-and-butter for structuring your business logic in a useful way. It's what you want to use to "prepare" the data necessary before the action executes.
request.referer gives you the previous URL or / if none. It is usually used to redirect the user back to the previous page (link)
By using the post method rather than the get method, Rails will define a route that will only respond to POST methods. The POST method is the typical method used by forms all over the web. You now need to create the create action within the PostsController for this to work.
The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.
View is already displayed on the user browser, so Sending Data from View to Controller is only that the user inserts Data. To send Data from View to Controller, it's the same as sending Data in a web service, so we can use GET / POST Request to send it.
Resque, Delayed Job, or Sidekiq should suit your needs.
You can find links to these three, and a couple more here: https://www.ruby-toolbox.com/categories/Background_Jobs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With