Rails 4,根据用户改变布局中的徽标

时间:2015-04-21 18:31:24

标签: html css ruby-on-rails-4 haml

在我的应用程序中,我想根据登录的用户更改徽标和其他默认图片。任何人都有一个很好的教程或知道如何做到这一点? 我一直在寻找这个,但没有太多关于它的信息。

我已完成用户登录系统,可以创建新角色。目前,角色不会做任何事情,但它存在。

我现在正在添加我的主要布局,用户模型和角色模型。我想要的是改变" wfront / logo"取决于当前用户角色。我认为它应该很简单,但我在rails上很新,这个应用程序是由另一个人编写的。

如果您需要更多信息,请告诉我。

views/layouts/application.html.haml
//this is my app layout and I want to change the logo dynamically 'wfront/logo'
    %html
      %head
        %title= full_page_title
        = render 'wfront/head'
        = csrf_meta_tags
      %body{ class: "fixed-header fixed-navigationx smart-style-0 #{cookies[:menu_state]}" }
        %header#header
          = render 'wfront/logo'
          = render 'wfront/context_select'
          = render 'wfront/subcontext_select'
          = render 'wfront/toggle_menu_button'
          = render 'wfront/user_info'
          .pull-right
            = render 'wfront/currency_select'
            = render 'wfront/date_select'
        %aside#left-panel
          = render 'wfront/nav_menu'
          %span.minifyme{ data: { action: 'minifyMenu' } }
            %i.fa.fa-arrow-circle-left.hit

        #main
          #ribbon
          #content
            .row
              = render 'wfront/title'
              .col-xs-12.col-sm-5.col-md-5.col-lg-7
                %ul#sparks
                  = render 'wfront/sparks'
                  = render 'wfront/exchange_rates'
            = yield

        .page-footer
          = render 'wfront/footer'

        = render 'wfront/quaam'
        = javascript_include_tag 'application', "data-turbolinks-track" => true
        = yield :scripts

        = render 'wfront/modal'

-

  models/user.rb
    # == Schema Information
    #
    # Table name: users
    #
    #  id                     :integer          not null, primary key
    #  email                  :string(255)      default(""), not null
    #  encrypted_password     :string(255)      default(""), not null
    #  reset_password_token   :string(255)
    #  reset_password_sent_at :datetime
    #  remember_created_at    :datetime
    #  sign_in_count          :integer          default(0), not null
    #  current_sign_in_at     :datetime
    #  last_sign_in_at        :datetime
    #  current_sign_in_ip     :string(255)
    #  last_sign_in_ip        :string(255)
    #  created_at             :datetime
    #  updated_at             :datetime
    #  name                   :string(255)
    #

    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :confirmable, :registerable, :timeoutable and :omniauthable
      devise :database_authenticatable, :lockable,
             :recoverable, :rememberable, :trackable, :validatable

      has_many :account_users
      has_many :accounts, through: :account_users
      has_many :taxes, -> { uniq }, through: :accounts

      validates :name, :email, presence: true, uniqueness: true

      include Concerns::Roleize
      roleize

      def primary_account
        self.accounts.first
      end
    end

-

   model/role.rb
    # == Schema Information
    #
    # Table name: roles
    #
    #  id            :integer          not null, primary key
    #  name          :string(255)
    #  resource_id   :integer
    #  resource_type :string(255)
    #  created_at    :datetime
    #  updated_at    :datetime
    #

    class Role < ActiveRecord::Base
      belongs_to :resource, polymorphic: true
      before_create :tableize_name

      def tableize_name
        self.name = self.name
      end

      def self.method_missing(name, *args, &block)
        roles = self.where(["name =?", name])
        if roles.any?
          scope roles.first.name.to_sym, ->{roles.first}
          return self.send(name.to_sym)
        end
        super
      end
    end

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用基于user.role的if / else语句?

- if @user.get_role == 'some_role'
  = render 'some-logo'
- elsif @user.get_role == 'other_role'
  = render 'other-logo'
- else
  = render 'logo' 

user.rb

def get_role
  Role.where(user_id: self.id).first
end