“未定义的方法`companies_path'”错误

时间:2011-11-29 08:08:05

标签: ruby-on-rails-3

当我访问'company / new'时,我收到以下错误:

undefined method `companies_path'
Extracted source (around line #1):
1: <%= form_for(@company) do |f| %>

但是当我访问'company / 1 / edit'(使用相同的表格)时,一切正常。 这是“新”和“编辑”的公司控制器:

def new
  @company = Company.new
end
def edit
  @company = Company.find(params[:id])
end

这是(部分)形式:

<%= form_for(@company) do |f| %>
<!-- Show errors -->
<%= render('layouts/form_errors', :object => @company) %>

我真的不明白错误信息,因为代码中没有使用'companies_path'吗?

更新:这是routes.rb:

  get "users_dashboard/show"
  get "login" => "sessions#new", :as => "login" 
  get "logout" => "sessions#destroy", :as => "logout"

  resources :company
  resources :relations
  resources :activities
  resources :contacts
  resources :notes
  resources :tasks
  resources :users
  resources :sessions

  get "site/index"
  get "site/features"
  get "site/dashboard"

  root :to => 'users_dashboard#show'

这是公司模式:

class Company < ActiveRecord::Base
has_many :users
has_many :relations
has_many :contacts, :through => :relations
has_many :notes, :through => :contacts
has_many :tasks, :through => :contacts
has_one :subscription

accepts_nested_attributes_for :subscription

attr_accessible :name, :address1, :address2, :zipcode, :city, :country, :email,      :website, :telephone, :twitter, :linkedin, :code

validates       :name, :address1, :zipcode, :city, :country, :code, presence: true
validates_length_of :code, :maximum => 3

1 个答案:

答案 0 :(得分:2)

你应该改变

resources :company

resources :companies