0

I have 2 controller API, A and B on Ruby on rails application. If the parameter passed the validation in controller A, it will redirect to create action on controller B. If it is not, then continue to run the logic on controller A.

At first, I try using redirect_to, but since the action is using POST method, it doesn't work. Then I try to use repost ruby gems, from https://rubygems.org/gems/repost. I have followed the documentation, but when I test the API, it returns No template found for xxxController#create, rendering head :no_content. Since it is an API, I think it doesn't need view.

I have tried to add view for that controller, but it ends up rendering the html to the response. Anyone have an idea to make it redirect to other controller? Or any other ways to solve this using other gems maybe?

EDIT I also try to render the html this way:

render html: Repost::Senpai.perform('http://xxx', params: { auth: request.params[:user] }, options: {authenticity_token: :auto, autosubmit: true}).html_safe, status: status

I copy the logic from the gem repo in https://github.com/vergilet/repost/blob/master/lib/repost/extend_controller.rb. But still, the html just rendered to the response. It doesn't redirect to given url

Intan
  • 143
  • 1
  • 2
  • 15

1 Answers1

0

The mechanism that makes repost work is that it's rendering an HTML form and auto-submitting it. Just calling Repost::Senpai.perform alone won't render the HTML.

You could try the following. It's based on repost's extension for ActionController::Base

render html: Repost::Senpai.perform(
  'http://xxxx',
  params: { auth: request.params[:user] },
).html_safe, status: :ok
TonyArra
  • 10,607
  • 1
  • 30
  • 46
  • when I used `repost`, I got `undefined method 'repost'` error. And refer to the answer in github issue https://github.com/vergilet/repost/issues/13, he said that if we use `ActionController::API` then we need to use Senpai, because `repost` is only for `ActionController::Base` – Intan Apr 14 '22 at 05:37
  • @Intan updated my answer - though I'm not sure if that will work. Using an API controller doesn't really make sense here because `repost` requires an HTML form to be rendered – TonyArra Apr 14 '22 at 05:55
  • I tried it, but it still won't redirect to the other API controller. The html from `Repost::Senpai.perform` is rendered to the response – Intan Apr 14 '22 at 06:01
  • @Intan yeah, it's not going to work. Repost creates an HTML form and then uses javascript to submit it. – TonyArra Apr 14 '22 at 06:09
  • @Intan an alternative method would be to call the controller_b.process(:create) from controller A instead of trying to do it as a redirect: https://stackoverflow.com/questions/5767222/rails-call-another-controller-action-from-a-controller – TonyArra Apr 14 '22 at 06:20