rails4将默认url xxx.com/new_page更改为xxx.com/new_page/username

时间:2018-05-01 10:01:39

标签: ruby-on-rails-4

我使用rails4.2.8ruby2.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

1 个答案:

答案 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)