【问题标题】:How to redirect at the route level, not at the action level?如何在路由级别而不是在操作级别重定向?
【发布时间】:2012-07-18 04:13:44
【问题描述】:

我想在路由级别 (config/routes.rb) 将 campaigns#index 重定向到 newsletters#index

config/routes.rb

#match '/campaigns' => redirect('/newsletters')             # something like this
resources :campaigns do
  put 'send_preview'
  put 'send_actual'
  put 'unschedule'
end

以下测试成功,但我希望它成功,因为路由级重定向,而不是操作级重定向。

测试/功能/campaigns_controller_test.rb

class CampaignsControllerTest < ActionController::TestCase

test "get index should redirect to newsletters" do
  get :index
  assert_redirected_to newsletters_path
end

app/controllers/campaigns_controller.rb

一旦路由级重定向工作,我想摆脱这个动作:

def index
  redirect_to newsletters_path
end

【问题讨论】:

  • match '/campaigns' =&gt; redirect('/newsletters') 是您从 routes.rb 进行重定向的方式。你在问什么?
  • 我不确定当时出了什么问题,但是是的,我的评论行确实有效。

标签: ruby-on-rails ruby-on-rails-3 testing redirect routes


【解决方案1】:

您的评论行

match '/campaigns' => redirect('/newsletters')

正在工作,而且

match '/campaigns', to: 'newsletters#index'

也在工作。

【讨论】:

    猜你喜欢
    • 2018-10-20
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多