设计用户中的路由问题
Routing Issue Within Devise Users
我是 Rails 的新手,正在使用 Devise 在新的演示应用程序中进行用户身份验证。我已经让 Devise 工作,连同可确认的模块和电子邮件验证。
我现在正尝试在此基础上构建一个 phone 验证。我更新了用户模型以包含三个附加字段:phone_number、phone_verified(布尔值)和 phone_verification_code。我还更新了注册控制器以允许用户模型添加 phone_number 的附加字段。
现在,为了设置一个 phone 验证系统,我通过在 UsersController 中添加一个验证方法并更新 routes.rb 创建了一个页面 /user/:id/verify .当我输入 URL、http://localhost:3000/users/10/verify 时,我会看到该页面的视图。
但是,我试图通过在 /app/views/users/show.html.erb 上创建一个按钮来访问视图,但我下面的代码出错了。我正在尝试为按钮获取正确的路径助手。有人可以帮忙吗?
这里是错误:
显示 /home/ubuntu/work/depot/app/views/users/show.html.erb 第 10 行出现的位置:
#<#:0x007fc3740fe3a0>
的未定义方法“verify_user_path”
app/views/users/show.html.erb
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-md-offset-2 col-md-8">
<div class="panel panel-default">
<div class="panel-heading"><%= @user.email %></div>
<div class="panel-body">
<strong>Phone number: </strong><%= @user.phone_number %><br/>
<% if @user.phone_verified == nil %>
<%= button_to [:verify, @user] do %>
Verify Phone <strong><%= @user.phone_number %></strong>
<% end %>
<% else %>
<strong>Phone Verified: </strong><%= @user.phone_verified %><br/>
<% end%>
</div>
</div>
</div>
</div>
</div>
用户控制器
class UsersController < ApplicationController
def index
@users = User.all
end
def show
begin
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access an invalid user: #{params[:id]}"
redirect_to store_url, notice: "Attempt to access an invalid user: #{params[:id]}"
else
respond_to do |format|
format.html # show html.erb
format.json { render json: @user }
end
end
end
def verify
end
def generate_pin
@user.phone_verification_code = rand(0000..9999).to_s.rjust(4, "0")
save
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
@user = nil
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params[:user]
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "registrations" }
resources :users, only: [:index, :show, :edit, :update]
resources :orders
resources :line_items
resources :carts
match "users/:id/verify" => "users#verify", via: [:get]
get 'store/index'
resources :products
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'store#index', as: 'store'
您需要在路线中使用 as
选项:
match "users/:id/verify" => "users#verify", via: [:get], as: 'verify_user'
然后重启你的服务器。这将告诉 Rails 创建适当的 URL 辅助方法:verify_user_path
和 verify_user_url
match "users/:id/verify" => "users#verify", as: 'verify_user', via: [:get]
或更好的方法:
resources :users, only: [:index, :show, :edit, :update] do
get :verify
end
我是 Rails 的新手,正在使用 Devise 在新的演示应用程序中进行用户身份验证。我已经让 Devise 工作,连同可确认的模块和电子邮件验证。
我现在正尝试在此基础上构建一个 phone 验证。我更新了用户模型以包含三个附加字段:phone_number、phone_verified(布尔值)和 phone_verification_code。我还更新了注册控制器以允许用户模型添加 phone_number 的附加字段。
现在,为了设置一个 phone 验证系统,我通过在 UsersController 中添加一个验证方法并更新 routes.rb 创建了一个页面 /user/:id/verify .当我输入 URL、http://localhost:3000/users/10/verify 时,我会看到该页面的视图。
但是,我试图通过在 /app/views/users/show.html.erb 上创建一个按钮来访问视图,但我下面的代码出错了。我正在尝试为按钮获取正确的路径助手。有人可以帮忙吗?
这里是错误:
显示 /home/ubuntu/work/depot/app/views/users/show.html.erb 第 10 行出现的位置:
#<#:0x007fc3740fe3a0>
的未定义方法“verify_user_path”app/views/users/show.html.erb
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-md-offset-2 col-md-8">
<div class="panel panel-default">
<div class="panel-heading"><%= @user.email %></div>
<div class="panel-body">
<strong>Phone number: </strong><%= @user.phone_number %><br/>
<% if @user.phone_verified == nil %>
<%= button_to [:verify, @user] do %>
Verify Phone <strong><%= @user.phone_number %></strong>
<% end %>
<% else %>
<strong>Phone Verified: </strong><%= @user.phone_verified %><br/>
<% end%>
</div>
</div>
</div>
</div>
</div>
用户控制器
class UsersController < ApplicationController
def index
@users = User.all
end
def show
begin
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access an invalid user: #{params[:id]}"
redirect_to store_url, notice: "Attempt to access an invalid user: #{params[:id]}"
else
respond_to do |format|
format.html # show html.erb
format.json { render json: @user }
end
end
end
def verify
end
def generate_pin
@user.phone_verification_code = rand(0000..9999).to_s.rjust(4, "0")
save
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
@user = nil
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params[:user]
end
end
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "registrations" }
resources :users, only: [:index, :show, :edit, :update]
resources :orders
resources :line_items
resources :carts
match "users/:id/verify" => "users#verify", via: [:get]
get 'store/index'
resources :products
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'store#index', as: 'store'
您需要在路线中使用 as
选项:
match "users/:id/verify" => "users#verify", via: [:get], as: 'verify_user'
然后重启你的服务器。这将告诉 Rails 创建适当的 URL 辅助方法:verify_user_path
和 verify_user_url
match "users/:id/verify" => "users#verify", as: 'verify_user', via: [:get]
或更好的方法:
resources :users, only: [:index, :show, :edit, :update] do
get :verify
end