rails4 将默认 url xxx.com/new_page 更改为 xxx.com/new_page/username [英] rails4 change default url xxx.com/new_page to xxx.com/new_page/username

查看:47
本文介绍了rails4 将默认 url xxx.com/new_page 更改为 xxx.com/new_page/username的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了rails4.2.8ruby2.5.0,当注册新用户时,我设置新页面跳转到http://127.0.0.1:3000/email_confirm ,但现在我想将 url 更改为 http://127.0.0.1:3000/email_confirm/username,即我想添加 username 到默认 url,我怎么能重写代码?非常感谢你的帮助~~

I used rails4.2.8 and ruby2.5.0, when signup the new user , I set the new page jump to http://127.0.0.1:3000/email_confirm ,but now I want to change the url to http://127.0.0.1:3000/email_confirm/username, that is I want to add username to the default url, How could I to rewrite the codes? Thanks for your help so much~~

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

app/views/users/email_confirm.html.erb

推荐答案

在您的 routes.rb 中,更改:

get 'email_confirm' => 'users#email_confirm'

到:

get 'email_confirm/:username' => 'users#email_confirm', as: 'confirmation'

然后,一旦用户注册成功,redirect_toconfirmation_path(@user.username)

Then, once the user signs up successfully, redirect_to confirmation_path(@user.username)

这篇关于rails4 将默认 url xxx.com/new_page 更改为 xxx.com/new_page/username的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆