充当追随者在前端不起作用
Acts As Follower doesn't work on front end
我安装了 Acts_as_follower。我在我的控制台中使用了文档中的方法,所以我确定用户 1 在 ActiveRecord 中跟随用户 2(使用设计),并且在 users/show.html.erb 页面的前端它显示了我实现的按钮的适当 follow/unfollow 部分。不幸的是,每当我按下 follow/unfollow 按钮时,都没有任何变化或发生。
我认为这是路由问题,但想知道是否有人知道为什么没有任何反应。我已从我的控制台确认缺少操作。
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
acts_as_followable
acts_as_follower
has_attached_file :image, :styles => { :medium => "300x300>", :thumb=> "100x100>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
has_many :articles
has_many :comments
end
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :articles do
member do
put "Like", to: "articles#upvote"
put "Disike", to: "articles#downvote"
end
resources :comments
end
resources :users do
get :follow
get :unfollow
end
root 'welcome#index'
users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@user_articles = @user.articles
end
def create
@user = User.find(params[:user_id])
current_user.follow(@user)
end
def destroy
@user = User.find(params[:user_id])
current_user.stop_following(@user)
end
end
followers_controller.rb
class FollowsController < ApplicationController
before_action :authenticate_user!
respond_to :js
def create
@user = User.find(params[:user_id])
current_user.follow(@user)
end
def destroy
@user = User.find(params[:user_id])
current_user.stop_following(@user)
end
end
users/show中的按钮。html.erb
<div class="follow">
<% if @user.followed_by?(current_user) %>
<%= form_tag user_unfollow_path(user_id: @user.id), method: :post, remote: true do %>
<center><%= button_tag 'unfollow', class: 'btn btn-primary' %></center>
<% end %>
<% else %>
<%= form_tag user_follow_path(user_id: @user.id), method: :post, remote: true do %>
<center><%= button_tag 'follow', class: 'btn btn-success' %></center>
<% end %>
<% end %>
</div>
</div>
你的怀疑是对的,确实是路由问题。但是你的努力得到了 A,因为文档没有讨论前端,你能走到这一步是件好事 :)
您的路线应该更像:
resources :users do
post :follow, to: "#followers#create"
delete :unfollow, to: "followers#destroy"
end
注意unfollow是调用destroy,所以按照惯例是delete,按照惯例create应该是post.
鉴于此,请确保您的视图如下所示:
<div class="follow">
<% if @user.followed_by?(current_user) %>
<%= form_tag user_unfollow_path(user_id: @user.id), method: :delete, remote: true do %>
<center><%= button_tag 'unfollow', class: 'btn btn-primary' %></center>
<% end %>
<% else %>
<%= form_tag user_follow_path(user_id: @user.id), method: :post, remote: true do %>
<center><%= button_tag 'follow', class: 'btn btn-success' %></center>
<% end %>
<% end %>
</div>
我安装了 Acts_as_follower。我在我的控制台中使用了文档中的方法,所以我确定用户 1 在 ActiveRecord 中跟随用户 2(使用设计),并且在 users/show.html.erb 页面的前端它显示了我实现的按钮的适当 follow/unfollow 部分。不幸的是,每当我按下 follow/unfollow 按钮时,都没有任何变化或发生。
我认为这是路由问题,但想知道是否有人知道为什么没有任何反应。我已从我的控制台确认缺少操作。
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
acts_as_followable
acts_as_follower
has_attached_file :image, :styles => { :medium => "300x300>", :thumb=> "100x100>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
has_many :articles
has_many :comments
end
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :articles do
member do
put "Like", to: "articles#upvote"
put "Disike", to: "articles#downvote"
end
resources :comments
end
resources :users do
get :follow
get :unfollow
end
root 'welcome#index'
users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@user_articles = @user.articles
end
def create
@user = User.find(params[:user_id])
current_user.follow(@user)
end
def destroy
@user = User.find(params[:user_id])
current_user.stop_following(@user)
end
end
followers_controller.rb
class FollowsController < ApplicationController
before_action :authenticate_user!
respond_to :js
def create
@user = User.find(params[:user_id])
current_user.follow(@user)
end
def destroy
@user = User.find(params[:user_id])
current_user.stop_following(@user)
end
end
users/show中的按钮。html.erb
<div class="follow">
<% if @user.followed_by?(current_user) %>
<%= form_tag user_unfollow_path(user_id: @user.id), method: :post, remote: true do %>
<center><%= button_tag 'unfollow', class: 'btn btn-primary' %></center>
<% end %>
<% else %>
<%= form_tag user_follow_path(user_id: @user.id), method: :post, remote: true do %>
<center><%= button_tag 'follow', class: 'btn btn-success' %></center>
<% end %>
<% end %>
</div>
</div>
你的怀疑是对的,确实是路由问题。但是你的努力得到了 A,因为文档没有讨论前端,你能走到这一步是件好事 :)
您的路线应该更像:
resources :users do
post :follow, to: "#followers#create"
delete :unfollow, to: "followers#destroy"
end
注意unfollow是调用destroy,所以按照惯例是delete,按照惯例create应该是post.
鉴于此,请确保您的视图如下所示:
<div class="follow">
<% if @user.followed_by?(current_user) %>
<%= form_tag user_unfollow_path(user_id: @user.id), method: :delete, remote: true do %>
<center><%= button_tag 'unfollow', class: 'btn btn-primary' %></center>
<% end %>
<% else %>
<%= form_tag user_follow_path(user_id: @user.id), method: :post, remote: true do %>
<center><%= button_tag 'follow', class: 'btn btn-success' %></center>
<% end %>
<% end %>
</div>