0

我的主要目标是通过参数哈希传递个人资料 ID 和公司 ID,以便更新 2 个模型之间的连接

模型已设置好,其中个人资料属于公司,公司有可能的员工(个人资料),如底部代码所示。

我想使用员工控制器来添加、删除和显示与公司的配置文件关联。

但是当我尝试这样做时,我收到以下错误

No route matches {:action=>"show", :controller=>"employees", :company_id=>#<Profile id: 1, user_id: 1, first_name: "Aaron", last_name: "Dufall", created_at: "2012-06-30 07:49:00", updated_at: "2012-07-01 14:03:24", deleted: false, company_id: 1>}

当我将删除指定为方法时,它何时想要显示,如下面的代码所示

显示.html.erb

<% @employees.each do |employee| %>
   <%= link_to full_name(employee), employee %>
   <%= link_to "Remove", company_employee_path(employee), :method => :delete %>
<% end %>

路线.rb

resources :companies do
  resources :employees
  resources :requests do
    put 'accept', :on => :member
  end
end

公司.rb

class Company < ActiveRecord::Base
  attr_accessible :name

  has_many :employees, 
           :foreign_key => 'company_id', 
           :class_name => "Profile"

  has_many :requests, as: :requestable

  validates :name,  presence: true, length: { maximum: 50 }, uniqueness: true

结尾

配置文件.rb

class Profile < ActiveRecord::Base
  belongs_to :user
  belongs_to :company
  has_many :requests
  has_many :requested, as: :requestable

  attr_accessible :first_name, :last_name


  validates :first_name, presence: true
  validates :last_name, presence: true

end
4

1 回答 1

0

在 routes.rb 中使用resource而不是。resources

于 2012-07-01T16:18:53.563 回答