Rails redirect_to渲染但不显示视图

时间:2016-01-15 15:14:46

标签: ruby-on-rails ruby rest

我正在尝试在我的控制器中执行某些操作后重定向到另一个视图

我的控制器

def sendPayment
@jsonParams = cleanParams(params)


@result = HTTParty.post('http://pxxxxxxxxxs'.to_str,
:body => @jsonParams.to_json,
:headers => { 'Content-Type' => 'application/json',
              'Api-Access-Key' => 'xxxxxxxx',
              'Transaction-Hash' => xxxx } )

 puts @result
 redirect_to payments_response_path(:result => @result)
end

在我的控制台中,我收到此消息:

 Processing by PaymentsController#show as JS
 Parameters: {"id"=>"response"}
 Rendered payments/show.html.erb within layouts/application (0.3ms)
 Completed 200 OK in 33ms (Views: 32.8ms | ActiveRecord: 0.0ms)

但我的观点从未呈现过......任何人都可以帮忙吗?

我的路线:

 Rails.application.routes.draw do
 get 'home/index'

 root 'home#index' 

 resources :home
 resources :payments
 resources :bills
 resources :accounts

 #Payment Custom Routes
 post 'payments/send', to: "payments#sendPayment"
 get 'payments/send', to: "payments#sendPayment"
 post 'payments/remove', to: "payments#deletePayment"
 get 'payments/remove', to: "payments#deletePayment"
 get 'payments/response', to: "payments#responseShow"
 #Bills Custom Routes
 post '/sendBill', to: "bills#sendBill"


 end

2 个答案:

答案 0 :(得分:0)

你正试图一次解决两个问题 - 我认为这是你在这里混淆的最大原因。重定向操作的工作方式是告诉浏览器“不,你想要这个URL”,并期望浏览器发出一个全新的请求;所以你应该单独对第二个请求进行故障排除,而不是提及重定向的任何内容。如果你的应用程序结构合理,那么对#show操作的请求是来自重定向还是仅仅是通过在浏览器中输入该URL来实现。

因此,请忽略重定向的内容,只关注:为什么我的#show动作不呈现任何内容?看起来你的日志告诉你没有错误,所以从你的布局开始(它们被调用了吗?它们是否正确显示在页面上?)并从那里向内工作。

答案 1 :(得分:0)

1)确保您的控制器中有responseShow方法,并且responseShow.html.erb文件夹中还有views/payments

2)尝试在return方法内redirect_to方法之后立即添加sendPayment

redirect_to payments_response_path(:result => @result)
return