|
We have the follow/unfollow button under these counts, but there are a few states we need to consider. First, we don’t want to show any button if the user either is viewing their own profile or if they’re not logged in. Second, we want to display an “Unfollow” button if the user already follows this profile’s owner.- <% if current_user and @user != current_user %>
- <% if current_user.following? @user %>
- <%= form_tag relationship_path, method: :delete do %>
- <%= submit_tag "Unfollow" %>
- <% end %>
- <% else %>
- <%= form_for @relationship do %>
- <%= hidden_field_tag :followed_id, @user.id %>
- <%= submit_tag "Follow" %>
- <% end %>
- <% end %>
- <% end %>
复制代码 A Rails resource is a basically a model, its associated controller, and a few other files.
Put this code just under the paragraph that holds the above spans.
The forms are the more complex parts here. First, if the current user already follows the viewed user, we’ll useform_tag to create a form that goes to therelationship_path. Of course, we can’t forget to set themethod as delete because we’re deleting a relationship.
|
|