我是Ruby的新手(2-3天)并在现有数据库上使用它。我有一个用户表(以前在Joomla中构建)并设法让它工作(好了,登录和退出),但我需要约束另一个模型,即'Leads'给登录的用户。
我已经能够获得所有当前的“潜在客户”输出并使用CRUD操作,但我想知道如何根据登录的用户限制数据。
'leads'模型是使用scaffolding生成的,并且'set_table_name'和'set_primary_key'属性已被覆盖。我还添加了
belongs_to :user,
:class_name => "user",
:foreign_key => "leadid"
到模特。
'用户'模型如下
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
`establish_connection :web_development
set_table_name "users"
set_primary_key "id"
has_many :leads,
:class_name => "Lead",
:foreign_key => "LeadID"
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
alias :devise_valid_password? :valid_password?
def valid_password?(password)
return true (temp to bypass the password)
end
end
非常感谢 鲁迪
答案 0 :(得分:0)
您可以将此行添加到您的leads_controller
before_filter :authenticate_user!
在允许访问该控制器中的任何操作之前,应该确保用户已经过适当的身份验证。
答案 1 :(得分:0)
Devise使用current_user
为当前登录的用户提供访问者。由于看起来您已正确设置关系,如果您只想在控制器的索引操作中显示登录用户的潜在客户,您只需要:
def index
@leads = current_user.leads
end