如何添加一条映射到由ruby on rails 3.1中的stringex gem生成的slug url的路由



这看起来很简单,在我的模型中我有:

class CustomerAccount < ActiveRecord::Base
  acts_as_url :name
  def to_param
    url # or whatever you set :url_attribute to
  end
end

在我的控制器中,我有:

class CustomerAccountsController < ApplicationController
  def show # dashboard for account, set as current account
    @account = CustomerAccount.find_by_url params[:id]
    no_permission_redirect if !@account.has_valid_user?(current_user)
    set_current_account(@account)
    @latest_contacts = Contact.latest_contacts(current_account)
  end
end

目前的路线.rb是:

  resources :customer_accounts, :path => :customer_accounts.url do
    member do
      get 'disabled'
      post 'update_billing'
    end
  end

当我试图通过rake-db生成数据时,这给了我以下错误:seed,或者至少我认为路由中的条目就是这样做的

undefined method `url' for :customer_accounts:Symbol

那么,我需要做些什么来设置路线呢?我想要的是http://0.0.0.0/customeraccountname以映射到客户帐户页面的视图。

更新:

以下是最终在routes.rb中工作的代码,我是在看了下面答案中的示例后发现的:

  resources :customer_accounts, :path => '/:id' do
    root :action => "show"
    member do
      get 'disabled'
      post 'update_billing'
    end
  end

如果你想设置它,这样你就有了一条如你所示的路线,那么就这样做:

get '/:id', :to => "customer_accounts#show"

如果您希望disabledupdate_billing操作位于以下位置:

get '/:id/disabled', :to => "customer_accounts#disabled"
post '/:id/update_billing', :to => "customer_accounts#update_billing"

或者(更整洁):

scope '/:id' do
  controller "customer_accounts" do
    root :action => "show"
    get 'disabled'
    get 'update_billing'
  end
end

最新更新