我在rails应用程序上有两个控制器API,A和B。如果参数通过了控制器A中的验证,它将重定向到控制器B上的create操作。如果不是,则继续在控制器A上运行逻辑。
起初,我尝试使用redirect_to,但是由于操作是使用POST方法,所以它不起作用。然后我尝试使用repost红宝石,来自https://rubygems.org/gems/repost。我遵循了文档,但是当我测试API时,它会返回No template found for xxxController#create, rendering head :no_content。因为它是一个API,所以我认为它不需要视图。
我尝试为该控制器添加视图,但它最终会将html呈现给响应。有人想让它重定向到其他控制器吗?或者用其他宝石解决这个问题的其他方法?
编辑我还尝试以这种方式呈现html:
render html: Repost::Senpai.perform('http://xxx', params: { auth: request.params[:user] }, options: {authenticity_token: :auto, autosubmit: true}).html_safe, status: status我从controller.rb中的gem回购中复制逻辑。但是,html仍然只是呈现给响应。它不会重定向到给定的url
发布于 2022-04-14 04:45:27
使repost工作的机制是它呈现一个HTML并自动提交它。仅仅调用Repost::Senpai.perform并不会呈现HTML。
你可以试试以下的方法。它基于repost的ActionController的扩展::Base
render html: Repost::Senpai.perform(
'http://xxxx',
params: { auth: request.params[:user] },
).html_safe, status: :okhttps://stackoverflow.com/questions/71866479
复制相似问题