Rails 2.3.8 “id”如何?传递给分页示例中的方法?
我正在学习 Michael Hartl 的“通过示例学习 Rails”教程(11.33)。相关代码是:
class PagesController < ApplicationController
def home
@title = "Home"
if signed_in?
@micropost = Micropost.new
@feed_items = current_user.feed.paginate(:page => params[:page])
end
end
class User < ActiveRecord::Base
def feed
Micropost.all(:conditions => ["user_id = ?", id])
end
module SessionsHelper
def sign_in(user)
user.remember_me!
cookies[:remember_token] = { :value => user.remember_token,
:expires => 20.years.from_now.utc }
self.current_user= user
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= user_from_remember_token
end
@feed_items 然后呈现为“will_paginate”的集合,一切正常。
我的问题是我无法理解 current_user 如何传递到 PagesController 行中的“feed”方法,即
“@feed_items = current_user.feed.paginate(:page => params[:page])”
任何帮助都会很大赞赏
艾伦
I'm working through "Learning Rails by Example" tutorial by Michael Hartl(11.33).The relevant code is:
class PagesController < ApplicationController
def home
@title = "Home"
if signed_in?
@micropost = Micropost.new
@feed_items = current_user.feed.paginate(:page => params[:page])
end
end
class User < ActiveRecord::Base
def feed
Micropost.all(:conditions => ["user_id = ?", id])
end
module SessionsHelper
def sign_in(user)
user.remember_me!
cookies[:remember_token] = { :value => user.remember_token,
:expires => 20.years.from_now.utc }
self.current_user= user
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= user_from_remember_token
end
@feed_items is then rendered as a collection to "will_paginate" and everything works fine.
My problem is that I cannot understand how the current_user is passed to the "feed" method in the PagesController line ie
"@feed_items = current_user.feed.paginate(:page => params[:page])"
Any help will be greatly appreciated
Alan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
current_user
永远不会“传递”到feed
方法 -feed
方法在current_user 上调用对象。看一下 User 模型:
这是一个实例方法,当您调用
current_user.feed
时执行。:conditions
哈希中的id
指的是用户对象实例的id
,在本例中为current_user
。current_user
is never "passed" to thefeed
method - thefeed
method is called on thecurrent_user
object. Take a look at the User model:This is an instance method which is executed when you call
current_user.feed
. Theid
in the:conditions
hash refers to theid
of the user object instance, in this casecurrent_user
.最有可能的“current_user”是在控制器的父类中定义的:ApplicationController。在控制器目录中搜索“application_controller.rb”。
Most likely "current_user" is defined in the paret class of your controller: ApplicationController. Search for "application_controller.rb" in your controller directory.