我使用rails4.2.8
和ruby2.5.0
,在注册新用户时,我将新页面设置为http://127.0.0.1:3000/email_confirm
,但现在我想将网址更改为http://127.0.0.1:3000/email_confirm/username
,我想将username
添加到默认网址,我怎么能重写代码?非常感谢你的帮助~~
这样的controllers/user_controller.rb
代码:
def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:success] = "almost success!"
redirect_to :email_confirm
else
flash.now[:danger] = 'fail to signup!'
render :new
end
end
def show
@user = User.find(params[:id])
end
这样的routes.rb
代码:
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
get 'profile' => 'users/show'
get 'email_confirm' => 'users#email_confirm'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
email_confirm.html.erb
app/views/users/email_confirm.html.erb
答案 0 :(得分:2)
在routes.rb
中,更改:
get 'email_confirm' => 'users#email_confirm'
为:
get 'email_confirm/:username' => 'users#email_confirm', as: 'confirmation'
然后,一旦用户成功注册,redirect_to confirmation_path(@user.username)