代码之家  ›  专栏  ›  技术社区  ›  Daniel Nelems

当试图循环浏览所有用户时,会为每个用户显示所有隐藏的表数据

  •  0
  • Daniel Nelems  · 技术社区  · 13 年前

    因此,我试图列出所有在我的网站上注册的用户,但我在我的视图页面上获得了每个用户的所有表格数据。

    用户控制器rb:

    class UsersController < ApplicationController
        before_filter :authenticate_user!
    
        def index
            @users = (current_user.blank? ? User.all : User.find(:all, :conditions => ["id != ?", current_user.id]))
      end
    
        def show
          if params[:id].nil? && current_user
            @user = current_user
          else
            @user = User.find(params[:id])
          end
          @cereal = current_user.cereals.build if signed_in?
          @cereals = @user.cereals.paginate(page: params[:page])
      end
    
      def first_time
          if params[:id].nil? && current_user
            @user = current_user
          else
            @user = User.find(params[:id])
          end
      end
    
        def edit
            if params[:id].nil? && current_user
                @user = current_user
            else
                @user = User.find(params[:id])
            end
      end
    
        def profile
            @username = params[:id]
            @friends = current_user.friends || []
            @title = "User Profile for #{@username}"
            @user = User.find_by_username(@username)
            @users = User.all :conditions => ["id != ?", current_user.id]
      end 
    
    end
    

    用户.rb(型号)

    class User < ActiveRecord::Base
        include Amistad::FriendModel
      mount_uploader :avatar, AvatarUploader
      has_many :cereals, dependent: :destroy
    
      # Include default devise modules. Others available are:
      # :token_authenticatable, :confirmable,
      # :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]
    
    
      # Setup accessible (or protected) attributes for your model
      attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :login, :avatar, :avatar_cache, :remove_avatar, :firstname, :lastname, :phone, :urlname
      # attr_accessible :title, :body
    
      attr_accessor :login
    
      def self.find_first_by_auth_conditions(warden_conditions)
          conditions = warden_conditions.dup
          if login = conditions.delete(:login)
            where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
          else
            where(conditions).first
          end
        end
    
                def update_with_password(params={})
            current_password = params.delete(:current_password)
    
            if params[:password].blank?
              params.delete(:password)
              params.delete(:password_confirmation) if params[:password_confirmation].blank?
            end
    
                    result = if params[:password].blank? || valid_password?(current_password) 
              update_attributes(params)
            else
              self.attributes = params
              self.valid?
              self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
              false
            end
    
            clean_up_passwords
            result
          end
        end
    

    视图/用户/索引:

    = @users.each do |user|
        %li
            = user.username
    

    有人知道为什么当我为用户打开索引页面时,数据库中用户表中的所有内容都会显示出来吗?

    1 回复  |  直到 13 年前
        1
  •  1
  •   Ryan    13 年前

    这是在快速浏览了您的代码之后,但我还是不假思索地尝试从索引视图中删除=。应该是

    - @users.each do |user|