【问题标题】:Routing Issue Within Devise Users设计用户内的路由问题
【发布时间】: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


    【解决方案1】:

    您需要在路由中使用as 选项:

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

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

    【讨论】:

    • 谢谢。我还必须将 :get 更改为 :post ,因为我使用的是 button_to 方法。那时它奏效了。
    【解决方案2】:
    match "users/:id/verify" => "users#verify", as: 'verify_user',  via: [:get]
    

    或者更好的方法:

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

    【讨论】:

    • 谢谢亚历山大。我能够使用第一种方法(使用 :post 而不是 :get),但不能使用第二种方法。我是否还必须在第二个选项中包含 :post 方法?
    • 你可以简单的添加方法::get for button_to helper。这将使用 get 方法生成表单。但是,经验法则是您的路线中的 :post 方法。
    猜你喜欢
    • 2013-04-14
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    相关资源
    最近更新 更多