所以我试图列出在我的网站上注册的所有用户,但我在我的视图页面上获取每个用户的所有表格数据。
用户控制器.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
任何人都知道为什么当我为用户打开索引页面时,数据库中用户表中的所有内容都被显示?