2015-09-22 55 views
0

我对Rails相当陌生,在新的演示应用中使用Devise进行用户身份验证。我有Devise工作,以及可确认的模块和电子邮件验证。设计用户中的路由问题

我正在尝试在此基础上构建电话验证。我更新了用户模型以具有三个附加字段:phone_number,phone_verified(布尔值)和phone_verification_code。我还更新了注册控制器以允许User模型的附加字段phone_number。

现在,为了建立一个电话验证系统,我通过在UsersController中添加一个验证方法并更新routes.rb创建了一个page/user /:id/verify。当我输入网址http://localhost:3000/users/10/verify时,我看到了该页面的视图。

但是,我想通过在/app/views/users/show.html.erb上创建一个按钮来查看视图,下面的代码出现错误。我正在尝试获取按钮的正确路径助手。有人可以请帮助。

以下是错误:

显示/home/ubuntu/work/depot/app/views/users/show.html.erb其中行#10提出:

为未定义的方法`verify_user_path” #<#:0x007fc3740fe3a0>

应用/视图/用户/ 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' 

回答

0

您需要使用as选项路线:

match "users/:id/verify" => "users#verify", via: [:get], as: 'verify_user' 

然后重新启动服务器。这将告诉Rails创建正确的URL辅助方法:verify_user_pathverify_user_url

+0

谢谢。我还必须更改:get to:post,因为我使用的是button_to方法。当时它工作。 – PiperWarrior

0
match "users/:id/verify" => "users#verify", as: 'verify_user', via: [:get] 

或者更好的办法:

resources :users, only: [:index, :show, :edit, :update] do 
    get :verify 
end 
+0

谢谢亚历山大。我能够得到第一种方法(使用:post和not:get),但不能使用第二种方法。我是否也必须在第二个选项中包含:post方法? – PiperWarrior

+0

你可以简单的添加方法::get button_to helper。这将使用get方法生成表单。但是,经验法则是在路线中使用post方法。 –

-1

所以,你的路线需要更新以获得verify_user_path帮手。

您可以使用此:

get "users/:id/verify", to: "users#verify", as: :user_verify 

或者你可以用这样的块开拓:users资源:

resources :users, only: [...] do 
    member do 
    get "verify", action: :verify 
    end 
end 

这里是轨道路线导向: http://guides.rubyonrails.org/routing.html