作为追随者不会在前端工作

时间:2016-04-11 23:21:29

标签: ruby-on-rails rubygems

我安装了Acts_as_follower。我使用了控制台文档中的方法,因此我确定用户1在ActiveRecord中跟随用户2(使用设计),在我的前端用户/ show.html.erb页面中显示了相应的关注/我已经实现了按钮的取消关注部分。不幸的是,每当我按下跟随/取消关注按钮时,没有任何变化或发生。

我认为这是路由,但想知道是否有人知道为什么没有发生。我已经确认我的控制台没有动作了。

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>

1 个答案:

答案 0 :(得分:0)

您的怀疑是正确的,这确实是一个路由问题。但是你得到了A的努力,因为文档没有讨论前端,对你这么做很好:)

您的路线应该更像:

resources :users do
  post :follow, to: "#followers#create"
  delete :unfollow, to: "followers#destroy"
end

请注意,unfollow正在调用destroy,因此按照约定删除它,同样按照惯例,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>