向用户添加特定角色以在页面上看到内容 [英] adding a specific roles to user to see something on the page

查看:71
本文介绍了向用户添加特定角色以在页面上看到内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我之前有这个问题,但是没有得到任何有用的信息,并且被卡住了.好的,我的应用程序是一个简单的ebay克隆,我在其中将用户分为角色ID为1和2的买方和卖方.当有人注册或登录时,他们被转移到该应用程序的索引页上,现在我希望买方能看到一些东西与索引页面上的卖方不同,为此,我尝试了if and else方法,但没有任何反应.做错什么了吗? 我还有其他方法可以解决这个问题.

although i had this question before but did not get anything useful and am stuck. Ok my app is a simple ebay clone, where i had divided users into roles buyers and sellers with role id 1 and 2. When anyone sign up or login they are transferred to the index page of the app, Now i want buyers to see something different then sellers on the index page and for that I had tried the if and else method but nothing happened. Is am doing anything wrong? I there any other way to get on this.

我的Index.html.erb文件

    <div class="col-md-12 text-right">
        <% if role_id == 1 %>
            <%= link_to "Add a new Product", new_product_path, class: "btn btn-success" %>
    </div>
    <% else %>
      <div class="col-md-8">
        <% @products.each do |productt| %>
    <div class="product">
    <h4><strong><font style="text-transform: capitalize;"><%= shipment.user.full_name%></strong></h4></font>
    <h5><strong>DESCRIPTION: </strong><%= product.description %></h5>
     <div class="thumbnail">
    <td><%= image_tag product.image.url(:medium)%></td>
      <div class="meta">
        <%= link_to time_ago_in_words(product.created_at) + " ago" %>
        <span class="admin"> 
          | <%= link_to "Show Details", product %>
       </span>
     </div>
    </div>
   </div>
  </div>
 <% end %>
<% end %>

我的种子.rb

['buyers', 'sellers'].each do |role|
    Role.where(name: role).first_or_create
end

推荐答案

您需要进一步研究系统结构.具体来说,您是在问如何将买家/卖家发送到不同的页面(这表明系统存在重大分歧),而我认为您实际上是在问

You need to study a little bit more about system structure. Specifically, you're asking how to send a buyer/seller to different pages (which indicates a major system divide), whereas I think you're really asking about authorization.

  • 授权 =授予用户执行某项操作的权限
  • 身份验证 =他们是用户吗?
  • Authorization = giving a user permission to do something
  • Authentication = are they a user at all?

我认为您应该具有基本级别的用户(没有角色"),并且能够为每个用户分配特定的授权功能.

I think you should have a base level user (no "roles"), with the ability to assign specific authorization ability to each user.

除了具有许可可以出售的用户外,这基本上将使每个用户成为买方.这样,您可以为用户分配一个级别",如果他们的数据集中有该级别,则允许他们执行操作.

This will basically make every user a buyer, except for ones who have permission to sell. This way, you can assign a "level" to your users, allowing them actions if they have that level in their dataset.

-

我会像这样设置它:

#app/models/user.rb
class User < ActiveRecord::Base
   # columns id | username | password | role_id | etc
   belongs_to :role
   def has_role? test_role
       role.id == test_role
   end
end

#app/models/role.rb
class Role < ActiveRecord::Base
   # columns id | name | description | etc
   has_many :users
end

这将使您能够调用以下内容:

This will give you the ability to call the following:

<%= link_to "Add a new Product", new_product_path, class: "btn btn-success" if current_user.has_role? "1" %>

这将使您能够向Role模型添加一系列角色,并为每个用户分配一个角色.这将定义用户可以访问的功能数量(即只有在级别为"2"时,他们才能出售的IE等).

This will give you the ability to add a series of roles to your Role model, allocating one to each user. This will define the amount of functionality that user will have access to (IE they can only sell if they are at level "2" etc).

这篇关于向用户添加特定角色以在页面上看到内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆