在每个微帖子上显示发送者和接收者的用户配置文件
我们希望为在我们网站上输入的每个微帖子提供发送者和接收者属性。邮件的发件人以及邮件的收件人。
换句话说,在每个用户看到的每个微帖子上,我们都需要内容,并且在帖子内容的上方或下方,我们希望显示发送者和接收者。我们还希望用户能够单击发送者或接收者并直接链接到该配置文件。
我们怎样才能做到这一点呢?我们对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了消除一些混乱并使其更加轨道化,我会选择:
对于给定的微帖子对象“@micropost”,这将允许在您的视图中进行类似的操作:
*这假设了有关用户对象和路由的一些事情,但是应该让你走上正确的道路。
To eliminate some confusion and make it a bit more railsy, I'd go with:
this will allow something like this in your view for a given Micropost object "@micropost":
*this assumes several things about the user object and routing, but should get you on the right path.