Rails 2.3.8 “id”如何?传递给分页示例中的方法?

发布于 2024-09-16 05:17:19 字数 1096 浏览 6 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

家住魔仙堡 2024-09-23 05:17:19

current_user 永远不会“传递”到 feed 方法 - feed 方法在 current_user 上调用对象。看一下 User 模型:

def feed
  Micropost.all(:conditions => ["user_id = ?", id])
end

这是一个实例方法,当您调用 current_user.feed 时执行。 :conditions 哈希中的 id 指的是用户对象实例的 id,在本例中为 current_user

current_user is never "passed" to the feed method - the feed method is called on the current_user object. Take a look at the User model:

def feed
  Micropost.all(:conditions => ["user_id = ?", id])
end

This is an instance method which is executed when you call current_user.feed. The id in the :conditions hash refers to the id of the user object instance, in this case current_user.

心舞飞扬 2024-09-23 05:17:19

最有可能的“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文