注册失败后,设计重定向到错误的路径

时间:2015-02-20 17:06:15

标签: ruby-on-rails devise

我为我的Rails应用程序设置了Devise,到目前为止一切正常。我唯一的问题是,在注册失败后,Devise会重定向到错误的路径。

这是测试时错误的样子:

Failures:

1) Register with wrong credentials
   Failure/Error: expect(page.current_path).to eq new_user_registration_path

   expected: "/register"
        got: "/"

我的测试看起来像这样:

feature 'Register' do
   scenario 'with wrong credentials' do
      visit new_user_registration_path
      fill_in 'First Name', with: ''
      fill_in 'Email', with: ''
      fill_in 'Password', with: ''
      fill_in 'Confirm Password', with: '...'
      click_on 'Register'

      expect(page.current_path).to eq new_user_registration_path
      expect(page).to have_content 'errors'
   end
end

这是我的路线:

devise_for :users,
           path: '/', 
           path_names: { sign_in: 'log_in', sign_out: 'log_out', sign_up: 'register'},
           controllers: { registrations: 'registrations'}

我尝试覆盖devise/registrations#create提到的def create build_resource(sign_up_params) resource.save yield resource if block_given? if resource.persisted? if resource.active_for_authentication? set_flash_message :notice, :signed_up if is_flashing_format? sign_up(resource_name, resource) respond_with resource, location: after_sign_up_path_for(resource) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format? expire_data_after_sign_in! respond_with resource, location: after_inactive_sign_up_path_for(resource) end else clean_up_passwords resource # instead of # respond_with resources # I wrote: redirect_to new_user_registration_path end end ,但最终丢失了默认的错误消息:

Ruby 2.2.0

我正在使用:Rails 4.2.0Devise 3.4.1和{{1}}。

1 个答案:

答案 0 :(得分:0)

尝试改变

devise_for :users,
           path: '/', 
           path_names: { sign_in: 'log_in', sign_out: 'log_out', sign_up: 'register'},
           controllers: { registrations: 'registrations'}

devise_for :users,
               path: '', 
               path_names: { sign_in: 'log_in', sign_out: 'log_out', sign_up: 'register'}

注意我删除了controllers: { registrations: 'registrations'}'/'

我认为path: '/'导致问题