【问题标题】:Rake routes 'match' mapping to the wrong action将路由“匹配”映射到错误的操作
【发布时间】:2013-02-21 21:47:00
【问题描述】:

我的问题与使用命名路由映射到控制器/操作有关。我正在尝试将“/profile”映射到“customers#show”。我的路线文件如下所示:

    root :to => 'pages#home'

  ## named routes
  match "profile" => "customers#show", :as => 'profile'
  match 'signin' => 'sessions#new', :as => 'signin'
  match 'signout' => 'sessions#destroy', :as => 'signout'

  resources :customers do
    member do 
      get 'add_card'
      post 'submit_card'
    end
  end


  resources :payments, :only => [:show, :new]
    delete  'payments/delete_recurring_payment'
    post    'payments/submit_non_recurring'
    post    'payments/submit_recurring'

  resources :sessions, :only => [:create, :destroy, :new] 

运行“rake routes”给了我这个:

                         root        /                                            pages#home
                      profile        /profile(.:format)                           customers#show
                       signin        /signin(.:format)                            sessions#new
                      signout        /signout(.:format)                           sessions#destroy
            add_card_customer GET    /customers/:id/add_card(.:format)            customers#add_card
         submit_card_customer POST   /customers/:id/submit_card(.:format)         customers#submit_card
                    customers GET    /customers(.:format)                         customers#index
                              POST   /customers(.:format)                         customers#create
                 new_customer GET    /customers/new(.:format)                     customers#new
                edit_customer GET    /customers/:id/edit(.:format)                customers#edit
                     customer GET    /customers/:id(.:format)                     customers#show
                              PUT    /customers/:id(.:format)                     customers#update
                              DELETE /customers/:id(.:format)                     customers#destroy
                  new_payment GET    /payments/new(.:format)                      payments#new
                      payment GET    /payments/:id(.:format)                      payments#show

这就是我难过的地方。当我转到 localhost:3000/profile 时,我收到一个路由错误:

No route matches {:action=>"edit", :controller=>"customers"}

这似乎很奇怪,因为由于我将客户声明为资源,确实有一条通往“customers#edit”的路线。

但是,当我转到“localhost:3000/signin”时,我会被路由到“customers#show”,这是我希望“/profile”路由到的地方。

似乎我的路线在我的路线文件中是“一次性”的,但我不知道为什么。任何帮助将不胜感激。

谢谢

更新 1:添加我的客户控制器

   class CustomersController < ApplicationController
  layout "payments_layout"

  def show
    @customer = current_user
    get_account_info(@customer)
    get_payment_history(@customer, 10)
  end

  def new
     @title = 'Create an account'
     @customer = Customer.new
  end

  def edit
    @customer = current_user
    get_account_info(@customer)
  end

  def update
    @customer = current_user
    if @customer.update(params[:customer])
      redirect_to @customer
    else
      @card_message = "Use this form to add a credit card to your account.  You must have a credit card associated with your account in
                      in order to make payments on our system."
      get_account_info(@customer)
      render 'edit'
    end
  end

  def create
   @customer = Customer.new(params[:customer])
   if @customer.save_and_get_stripe_id
     sign_in(@customer)
     redirect_to @customer   
   else
     @title = 'Create an account'
     render 'new'
   end
  end

  def add_card
    @customer = current_user
    get_account_info(@customer)
    @card_message = "Use this form to add a credit card to your account.  You must have a credit card associated with your account in
                    in order to make payments on our system."
  end

  def submit_card
    @customer = current_user
    res = @customer.add_or_update_card(params)
    if res
      redirect_to @customer
    else
      @error = res
      customer.get_account_info(@customer)
      render 'add_card'
    end
  end

end

【问题讨论】:

  • 你能告诉你的客户控制器吗?

标签: ruby-on-rails ruby rails-routing


【解决方案1】:

检查您的意见。

您有编辑用户资料的链接吗?

看起来您实际上路由到了正确的控制器和操作,但有一些 rake 无法路由的链接,例如您没有将 ID 传递给您的 edit_customer_path 链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 2022-08-16
    • 1970-01-01
    • 2012-05-24
    相关资源
    最近更新 更多