###前提・実現したいこと
rails4で開発中。
現在練習でツイッターのような投稿サイトを作成しています。
フォロー機能を実装してログインしたユーザー同士フォローできるようにしました。
ユーザー一覧からフォローやフォローの解除はできたのですが、ユーザー毎のマイページにアクセスした時にもフォロー又はフォロー解除ボタンを実装したいと思っています。
ですが、マイページ(users/show.html.erb)でのフォローボタンの実装がうまくできないでいます。
どのようにしたらいいかご教授頂けると助かります。
###該当のソースコード
users_controller.rb
def show @user = User.find(params[:id]) end
views/users/_follow_form.html.erb
<div id="follow_form_<%= user.id %>"> <% unless current_user.following?(user) %> <%= form_for(current_user.relationships.build(followed_id: user.id), remote: true) do |f| %> <%= f.hidden_field :followed_id %> <%= button_tag( class: "btn btn-sm btn-primary") do %> <%= content_tag :span, "フォロー", class: "fa fa-user-plus" %> <% end %> <% end %> <% else %> <%= form_for(current_user.relationships.find_by(followed_id: user.id), html: { method: :delete }, remote: true) do |f| %> <%= button_tag( class: "btn btn-sm") do %> <%= content_tag :span, "フォロー解除", class: "fa fa-user-times" %> <% end %> <% end %> <% end %> </div>
views/users/index.html.erb
<% @users.each do |user| %> <% if user.id != current_user.id %> <%= profile_img(user) %> <%= link_to user.name, user %> <ul class="nav navbar-nav navbar-right"> <li><%= link_to 'メッセージ', conversations_path(sender_id: current_user.id, recipient_id: user.id), method: 'post' %></li> <li class="li-friends"><%= render partial: 'follow_form', locals: { user: user } if signed_in? %></li> </ul> <% end %> <% end %>
現在、ユーザー一覧画面ではこのように記述しています。
このフォロー又はフォロー解除をユーザー毎のマイページに表示するにはどのようにしたらいいのでしょうか?
分かりづらい部分もあるかとは思いますが、ご教授お願いします。
###補足情報
多対多の関係を実装するために、Relationshipモデルを作成し中間テーブルを用意してフォロー機能は実装しました。
他に何か必要な該当のソースコードがあれば追記致しますので、その際は仰ってください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/03 04:45