代码之家  ›  专栏  ›  技术社区  ›  Jesse Sravya

Rails,更改控制器内POST操作的URL

  •  0
  • Jesse Sravya  · 技术社区  · 8 年前

    我希望在控制器内调用操作时更改URL。我当前的场景:

    控制器:

    class EventController < ApplicationController
        def index 
            if city.blank?
                failure
            end
            ...
        end
    
        def failure
            ...
            render 'failure'
        end
    end
    

    路线:

    get '/event', to: 'event#index'
    post '/event/failure' => 'event#failure'
    

    但此代码将url保留为/事件。预期结果是/事件/失败

    我有付款“索引”和“失败”的视图。我正在使用rails ~ 5.0.0。

    1 回复  |  直到 8 年前
        1
  •  0
  •   bitsapien    8 年前

    要更改URL,您需要执行重定向,如下所示:

    class EventController < ApplicationController
        def index 
            if city.blank?
                redirect_to action: 'failure'
            end
            ...
        end
    
        def failure
            ...
            render 'failure'
        end
    end
    

    但是,如中所示,POST请求不可能重定向 HTTP/1.1