I follow the railstutorial.ru tutorial. I do everything as it is written but one error appeared, the follow button works and the unfollow button does not.
Here is the code.
sample_app / app / views / users / _unfollow.html.erb
<%= form_for(current_user.relationships.find_by(followed_id: @user), html: { method: :delete }, remote: true) do |f| %> <%= f.submit "Unfollow", class: "btn btn-large" %> <% end %> sample_app / app / views / users / _follow.html.erb
<%= form_for(current_user.relationships.build(followed_id: @user.id), remote: true) do |f| %> <div><%= f.hidden_field :followed_id %></div> <%= f.submit "Follow", class: "btn btn-large btn-primary" %> <% end %> sample_app / app / controllers / relationships.rb
class RelationshipsController < ApplicationController before_action :signed_in_user def create @user = User.find(params[:relationship][:followed_id]) current_user.follow!(@user) respond_to do |format| format.html { redirect_to @user } format.js end end def destroy @user = Relationship.find(params[:id]).followed current_user.unfollow!(@user) respond_to do |format| format.html { redirect_to @user } format.js end end end sample_app / app / views / relationships / create.js.erb
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>") $("#followers").html('<%= @user.followers.count %>') sample_app / app / views / relationships / destroy.js.erb
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>") $("#followers").html('<%= @user.followers.count %>') PS Here is a tutorial railstutorial.org
PPS Chapter 11.2.5
rails server/rails slaunched from. - D-side