【发布时间】:2015-09-22 20:02:46
【问题描述】:
我对 Rails 还很陌生,我在一个新的演示应用程序中使用 Devise 进行用户身份验证。我已经让 Devise 工作了,还有可确认的模块和电子邮件验证。
我现在正尝试在此基础上构建电话验证。我更新了 User 模型,增加了三个字段:phone_number、phone_verified(布尔值)和 phone_verification_code。我还更新了 Registrations Controller 以允许 User 模型的 phone_number 附加字段。
现在,为了设置电话验证系统,我通过在 UsersController 中添加验证方法并更新 routes.rb 来创建页面 /user/:id/verify。当我输入 URL http://localhost:3000/users/10/verify 时,我会看到该页面的视图。
但是,我试图通过在 /app/views/users/show.html.erb 上创建一个按钮来访问该视图,并且我的下面的代码出现错误。我正在尝试为按钮获取正确的路径助手。有人可以帮忙吗。
这是错误:
显示第 10 行引发的 /home/ubuntu/work/depot/app/views/users/show.html.erb:
#的未定义方法`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>
UsersController
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'
【问题讨论】:
标签: ruby-on-rails ruby devise routes