Ruby on rails 4 为什么我不能在rails索引视图中显示所有数据库结果?
我被困在Rails教程中。这是我的控制器:Ruby on rails 4 为什么我不能在rails索引视图中显示所有数据库结果?,ruby-on-rails-4,railstutorial.org,Ruby On Rails 4,Railstutorial.org,我被困在Rails教程中。这是我的控制器: class UsersController < ApplicationController before_action :signed_in_user, only: [:index, :edit, :update] before_action :correct_user, only: [:edit, :update] def index @users = User.all end def show @user = User.find(para
class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update]
before_action :correct_user, only: [:edit, :update]
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params) #@user = User.new(params[:user])
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def edit
#not needed anymore due to before_action: @user = User.find(params[:id])
end
def update
# not needed anymore: @user = User.find(params[:id])
if @user.update_attributes(user_params)
# Handle a successful update:
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
# Before filters
def signed_in_user
store_location
redirect_to signin_url, notice: "Please sign in" unless signed_in?
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
end
然而,该视图无法找到它们。它只找到一个用户,这是我通过rake任务输入99个用户之前的情况。以下是服务器日志:
Started GET "/users" for 127.0.0.1 at 2014-05-28 17:07:32 +0100
Processing by UsersController#index as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'cd96a5eb437ae73a82b73e2f2ae20de90dad6da7' LIMIT 1
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from _app_views_users_index_html_erb__4537893108263142400_70344304651460 at /Users/nnikolo/Documents/private/rails_projects/sample_app/app/views/users/index.html.erb:5)
User Load (0.1ms) SELECT "users".* FROM "users"
users/index: @users.all.count = 1
CACHE (0.0ms) SELECT "users".* FROM "users"
Rendered users/index.html.erb within layouts/application (2.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 15ms (Views: 14.0ms | ActiveRecord: 0.3ms)
我怀疑有某种缓存,但根据日志,只有第二个查询SELECT users.*FROM users被缓存。出于调试目的而进行的第一次调试似乎是从数据库而不是缓存中提供的。但是它只找到一条记录,这是users/index:@users.all.count=1行。我做错了什么?我想我是准确地按照说明操作的。我刚刚重新启动了WEBrick服务器,现在查询按预期工作:
W, [2014-05-28T18:27:34.485654 #91837] WARN -- : DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from _app_views_users_index_html_erb___3901190429481931463_70221243438200 at /Users/nnikolo/Documents/private/rails_projects/sample_app/app/views/users/index.html.erb:5)
D, [2014-05-28T18:27:34.486359 #91837] DEBUG -- : User Load (0.4ms) SELECT "users".* FROM "users"
D, [2014-05-28T18:27:34.486387 #91837] DEBUG -- : User Load (0.4ms) SELECT "users".* FROM "users"
D, [2014-05-28T18:27:34.519286 #91837] DEBUG -- : users/index: @users.all.count = 100
D, [2014-05-28T18:27:34.519349 #91837] DEBUG -- : users/index: @users.all.count = 100
D, [2014-05-28T18:27:34.519619 #91837] DEBUG -- : CACHE (0.0ms) SELECT "users".* FROM "users"
D, [2014-05-28T18:27:34.519647 #91837] DEBUG -- : CACHE (0.0ms) SELECT "users".* FROM "users"
I, [2014-05-28T18:27:34.537062 #91837] INFO -- : Rendered users/index.html.erb within layouts/application (52.0ms)
I, [2014-05-28T18:27:34.537116 #91837] INFO -- : Rendered users/index.html.erb within layouts/application (52.0ms)
I, [2014-05-28T18:27:34.545021 #91837] INFO -- : Rendered layouts/_shim.html.erb (0.0ms)
I, [2014-05-28T18:27:34.545065 #91837] INFO -- : Rendered layouts/_shim.html.erb (0.0ms)
I, [2014-05-28T18:27:34.546015 #91837] INFO -- : Rendered layouts/_header.html.erb (0.5ms)
I, [2014-05-28T18:27:34.546043 #91837] INFO -- : Rendered layouts/_header.html.erb (0.5ms)
I, [2014-05-28T18:27:34.546629 #91837] INFO -- : Rendered layouts/_footer.html.erb (0.1ms)
I, [2014-05-28T18:27:34.546655 #91837] INFO -- : Rendered layouts/_footer.html.erb (0.1ms)
I, [2014-05-28T18:27:34.547216 #91837] INFO -- : Completed 200 OK in 65ms (Views: 63.1ms | ActiveRecord: 0.6ms)
I, [2014-05-28T18:27:34.547243 #91837] INFO -- : Completed 200 OK in 65ms (Views: 63.1ms | ActiveRecord: 0.6ms)
我的问题是为什么我必须重新启动服务器?本教程根本没有提到需要这样做,具体如清单9.27所示。我也不记得上次服务器重新启动后更改了数据库结构。我是一名rails新手,但对我来说,如果每次插入数据库后都需要重新启动服务器,那听起来就大错特错了。将控制器代码发布到@users的定义位置。你能提供你正在关注的rails教程第9章的链接吗?@Pavan:我只是将链接放在我文章的第一行,我指的是第9章
Started GET "/users" for 127.0.0.1 at 2014-05-28 17:07:32 +0100
Processing by UsersController#index as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'cd96a5eb437ae73a82b73e2f2ae20de90dad6da7' LIMIT 1
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from _app_views_users_index_html_erb__4537893108263142400_70344304651460 at /Users/nnikolo/Documents/private/rails_projects/sample_app/app/views/users/index.html.erb:5)
User Load (0.1ms) SELECT "users".* FROM "users"
users/index: @users.all.count = 1
CACHE (0.0ms) SELECT "users".* FROM "users"
Rendered users/index.html.erb within layouts/application (2.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 15ms (Views: 14.0ms | ActiveRecord: 0.3ms)
W, [2014-05-28T18:27:34.485654 #91837] WARN -- : DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from _app_views_users_index_html_erb___3901190429481931463_70221243438200 at /Users/nnikolo/Documents/private/rails_projects/sample_app/app/views/users/index.html.erb:5)
D, [2014-05-28T18:27:34.486359 #91837] DEBUG -- : User Load (0.4ms) SELECT "users".* FROM "users"
D, [2014-05-28T18:27:34.486387 #91837] DEBUG -- : User Load (0.4ms) SELECT "users".* FROM "users"
D, [2014-05-28T18:27:34.519286 #91837] DEBUG -- : users/index: @users.all.count = 100
D, [2014-05-28T18:27:34.519349 #91837] DEBUG -- : users/index: @users.all.count = 100
D, [2014-05-28T18:27:34.519619 #91837] DEBUG -- : CACHE (0.0ms) SELECT "users".* FROM "users"
D, [2014-05-28T18:27:34.519647 #91837] DEBUG -- : CACHE (0.0ms) SELECT "users".* FROM "users"
I, [2014-05-28T18:27:34.537062 #91837] INFO -- : Rendered users/index.html.erb within layouts/application (52.0ms)
I, [2014-05-28T18:27:34.537116 #91837] INFO -- : Rendered users/index.html.erb within layouts/application (52.0ms)
I, [2014-05-28T18:27:34.545021 #91837] INFO -- : Rendered layouts/_shim.html.erb (0.0ms)
I, [2014-05-28T18:27:34.545065 #91837] INFO -- : Rendered layouts/_shim.html.erb (0.0ms)
I, [2014-05-28T18:27:34.546015 #91837] INFO -- : Rendered layouts/_header.html.erb (0.5ms)
I, [2014-05-28T18:27:34.546043 #91837] INFO -- : Rendered layouts/_header.html.erb (0.5ms)
I, [2014-05-28T18:27:34.546629 #91837] INFO -- : Rendered layouts/_footer.html.erb (0.1ms)
I, [2014-05-28T18:27:34.546655 #91837] INFO -- : Rendered layouts/_footer.html.erb (0.1ms)
I, [2014-05-28T18:27:34.547216 #91837] INFO -- : Completed 200 OK in 65ms (Views: 63.1ms | ActiveRecord: 0.6ms)
I, [2014-05-28T18:27:34.547243 #91837] INFO -- : Completed 200 OK in 65ms (Views: 63.1ms | ActiveRecord: 0.6ms)