错误:
nil:NilClass 的未定义方法“客户”
app/controllers/customers_controller.rb:5:in `index'
关于文档,以下方法将在设置关系时可用。但是控制器只是抛出错误。有什么想法或建议吗?
def index
@customers = @current_user.customers
respond_to do |format|
format.html # index.html.erb
format.json { render json: @customers }
end
end
这是我的简单客户模型:
class Customer < ActiveRecord::Base
attr_accessible :customerID, :first_name, :phone, :surname
belongs_to :user
end
而我的用户模型,主要是从设计中生成的。
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :role_ids, :as => :admin
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
validates_presence_of :name
validates_uniqueness_of :email, :case_sensitive => false
has_many :customers
end
那就是 index.erb
<% @customers.each do |customer| %>
<tr>
<td><%= customer.customerID %></td>
<td><%= customer.surname %></td>
<td><%= customer.first_name %></td>
<td><%= customer.phone %></td>
<td><%= link_to 'Show', customer %></td>
<td><%= link_to 'Edit', edit_customer_path(customer) %></td>
<td><%= link_to 'Destroy', customer, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>