サイトに入力された各マイクロポストの送信者属性と受信者属性が必要です。郵便の送付者とそれが指揮される受取人。各マイクロポストに送信者と受信者のユーザープロファイルを表示
言い換えれば、各ユーザーが見る各マイクロポストでは、送信者の表示と受信者に表示させる内容のコンテンツの上または下のコンテンツが必要です。ユーザーは、送信者または受信者のいずれかをクリックして、そのプロファイルに直接リンクすることもできます。
これを行うにはどうすればいいですか?私たちはレールが比較的新しく、この変更が機能するには、Micropostモデルで追加が必要であると考えています。または、MicropostsControllerで変更を加える必要がありますか?
Micropostモデル:
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 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
この意志を:
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
'belongs_to:user' - 送信者か受信者ですか? –
'belongs_to:user' - はログインしているユーザーです。投稿した投稿はプロフィールに属します。 –
我々は2つの属性、 'belongs_to_id'(受信者)' user_id'(送信者)で設定されたマイクロポストモデルを持っています。各マイクロポストに対して、投稿が送信されたユーザーの名前へのリンクを追加したいと考えています。言い換えれば、投稿の 'belongs_to_id'を' user_id'にマッチさせて、そのユーザのプロフィールへのリンクを作りたいと考えています –