在每个微帖子上显示发送者和接收者的用户配置文件

发布于 2025-01-03 01:50:11 字数 1611 浏览 0 评论 0原文

我们希望为在我们网站上输入的每个微帖子提供发送者和接收者属性。邮件的发件人以及邮件的收件人。

换句话说,在每个用户看到的每个微帖子上,我们都需要内容,并且在帖子内容的上方或下方,我们希望显示发送者和接收者。我们还希望用户能够单击发送者或接收者并直接链接到该配置文件。

我们怎样才能做到这一点呢?我们对 Rails 还比较陌生,认为需要在 Micropost 模型中添加一些内容才能使此更改发挥作用。或者应该在 MicropostsController 中进行更改?

微帖子模型:

class Micropost < ActiveRecord::Base
  attr_accessible :content, :belongs_to_id

  belongs_to :user


  validates :content, :presence => true, :length => { :maximum => 240 }
  validates :user_id, :presence => true

  default_scope :order => 'microposts.created_at DESC'

    # Return microposts from the users being followed by the given user.
    scope :from_users_followed_by, lambda { |user| followed_by(user) }

    private

      # Return an SQL condition for users followed by the given user.
      # We include the user's own id as well.
      def self.followed_by(user)
        following_ids = %(SELECT followed_id FROM relationships
                      WHERE follower_id = :user_id)
        where("user_id IN (#{following_ids}) OR user_id = :user_id",
              { :user_id => user })
    end
  end

微帖子控制器:

class MicropostsController < ApplicationController
  before_filter :authenticate, :only => [:create, :destroy]

  def create

    @micropost  = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Posted!"
      redirect_to current_user
    else
     @feed_items = []
     render 'pages/home'
    end
  end


  def destroy
      @micropost.destroy
      redirect_to root_path
    end
end

We are looking to have Sender and Receiver attributes for each micropost that is entered on our site. The sender of the post, and the receiver whom it is directed to.

In other words, on each micropost that each user sees, we want the content, and just above or below the content of the post we want the sender shown and receiver shown. We also want users to be able to click on either the sender or the receiver and be linked directly to that profile.

How can we go about doing this? We are relatively new to rails and think additions need to be made in the Micropost model for this change to work. Or should the changes be made in the MicropostsController?

Micropost Model:

class Micropost < ActiveRecord::Base
  attr_accessible :content, :belongs_to_id

  belongs_to :user


  validates :content, :presence => true, :length => { :maximum => 240 }
  validates :user_id, :presence => true

  default_scope :order => 'microposts.created_at DESC'

    # Return microposts from the users being followed by the given user.
    scope :from_users_followed_by, lambda { |user| followed_by(user) }

    private

      # Return an SQL condition for users followed by the given user.
      # We include the user's own id as well.
      def self.followed_by(user)
        following_ids = %(SELECT followed_id FROM relationships
                      WHERE follower_id = :user_id)
        where("user_id IN (#{following_ids}) OR user_id = :user_id",
              { :user_id => user })
    end
  end

MicropostsController:

class MicropostsController < ApplicationController
  before_filter :authenticate, :only => [:create, :destroy]

  def create

    @micropost  = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Posted!"
      redirect_to current_user
    else
     @feed_items = []
     render 'pages/home'
    end
  end


  def destroy
      @micropost.destroy
      redirect_to root_path
    end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你怎么敢 2025-01-10 01:50:11

为了消除一些混乱并使其更加轨道化,我会选择:

class Micropost < ActiveRecord::Base
  belongs_to :sending_user, :class_name=>"User", :foreign_key=>"user_id"
  belongs_to :receiving_user, :class_name=>"User", :foreign_key=>"belongs_to_id"
end

对于给定的微帖子对象“@micropost”,这将允许在您的视图中进行类似的操作:

 <%= link_to(@micropost.sending_user.username, user_path(@micropost.sending_user)) %>

 <%= link_to(@micropost.receiving_user.username, user_path(@micropost.receiving_user)) %>

*这假设了有关用户对象和路由的一些事情,但是应该让你走上正确的道路。

To eliminate some confusion and make it a bit more railsy, I'd go with:

class Micropost < ActiveRecord::Base
  belongs_to :sending_user, :class_name=>"User", :foreign_key=>"user_id"
  belongs_to :receiving_user, :class_name=>"User", :foreign_key=>"belongs_to_id"
end

this will allow something like this in your view for a given Micropost object "@micropost":

 <%= link_to(@micropost.sending_user.username, user_path(@micropost.sending_user)) %>

 <%= link_to(@micropost.receiving_user.username, user_path(@micropost.receiving_user)) %>

*this assumes several things about the user object and routing, but should get you on the right path.

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