Twitterもどき、monologue制作の続き。
id:NeoCat:20080528で作ったフォロー機能を使って、他のユーザをフォローするためのインターフェースをユーザ情報ビューに入れます。
app/users/index.rhtml の発言フィールドの部分を次のようにしておきます。既にフォロー中かどうかをチェックして、フォローする/フォロー解除のいずれかを表示させます。
<% if current_user == @user %> <div id="update_status"> <p><% form_for :status, :url => { :controller => :status, :action => :new } do |f| %> <p><label for="status">めっせーじ?<small>(140文字以内)</small></label><br> <%= f.text_area :status, :cols => 70, :rows => 4 %><br> <%= submit_tag "送信" %></p> <% end %></p> </div> <% else %> <%= icon_tag(@user, 64, :align => "left") %><strong> <%= h @user.login %></strong> <%= h @user.name %> <% if current_user.followees.member? @user %> <%= link_to "[ フォロー解除 ]", :controller => :follow, :action => :delete, :id => @user.login %> <% else %> <%= link_to "[ フォローする ]", :controller => :follow, :action => :new, :id => @user.login %> <% end %> <br> <big><%= s = @user.new_status && Status.find_by_id(@user.new_status); s ? h(s.status) : "" %></big> <% end %>
@userがcurrent_userからFollow中かどうかの判定は
current_user.followees.member?(@user)
でOK。読んだままですね。rubyってこういうときによく出来てるなーと思う。