English 中文(简体)
我怎么能够接触到我的铁路座标中的“座标”?
原标题:How can I access "roles" in "views" in my Rails app?

在铁路3中,Im大楼用于帮助我了解Ruby(和)铁路,Im被“roles/roles_users/users”表混为一谈。

我有德智和坎桑,我希望“发挥”这些作用。

现在,我有三个作用:

admin
staff
client

在数据库中,我有这些表格(以及其他几个表格):

roles (table)
id => 1 || name => admin
id => 2 || name => staff
id => 3 || name => client

roles_users(table)
role_id => 1 || user_id => 1
role_id => 2 || user_id => 2
role_id => 3 || user_id => 3

users(table)
user1
user2
user3

因此,现在我有三个用户。 第1号用户为“行政”,第2号用户为“工作人员”,第3号用户为“客户”。

在“观点/管理/用户/show.html.erb”中,我想显示每个用户的“role”,然后也允许行政部门改变。 一旦我能够发挥这一作用,我认为我可以设立这一机构,以便行政能够改变它。

...... 我对我能用来显示每个用户作用的变数/名称感到困惑不解。 我可以方便地接触用户等东西,首先——姓名或用户。

任何建议?

Cheers!

Sam

问题回答

我猜测你可以做@user.roles。 情况是,我猜测每个用户只发挥一种作用(尽管它具有一种算术关系),因此你可以在你的用户模式中创造:

def role
  roles.first
end

def role=(role_id)
  self.roles = [Role.find(role_id)]
end

CanCan公约将创建role?方法。 我知道,每个用户只发挥一种作用,但如果你后来决定每个用户发挥多种作用,这将对你们发挥作用:

def role?(role)
  !!self.roles.find_by_name(role.to_s)
end

这样,你就可以与以下各方核对授权:

if user.role? :editor
  can :manage, Foo
end

阅读:如果用户有<条码>><> >> >的作用,则 然后,他们可以管理 F模式。

@user.roles # returns a collection of roles for the given user

try: @user.roles.first.name





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签