无法使用“关注”按钮与Acts_As_Follower一起使用

时间:2017-01-19 17:02:42

标签: ruby-on-rails ruby rubygems ruby-on-rails-5

我正在使用我正在处理的心愿单应用中的Acts_as_follower。我认为它应该工作,但实际上跟随另一个用户的按钮不是。跟随按钮位于我的用户索引视图中,我可以在那里看到它,但它没有响应被点击。任何帮助,将不胜感激。这是我的代码:

index.html.erb

<div class="container">
  <% @users.each do |u| %>
    <div class="col-sm-8 col-lg-4">
      <div id="gifts">
        <div class="box panel panel-default">
          <h2><%= link_to u.name, u %></h2>
          <p>
          <table align="center" margin="5">
            <tr>
              <th style="padding-right: 10px"><%= button_to "View Wishlist", u, method: :get, class: "btn btn-primary" %></th>
              <th>
                <% if current_user.following?(u) %>
                  <%= form_tag user_unfollow_path(user_id: u.id), method: :delete, remote: true do %>
                    <%= button_tag 'unfollow', class: 'btn btn-primary' %>
                  <% end %>
                <% else %>
                  <%= form_tag user_follow_path(user_id: u.id), method: :post, remote: true do %>
                    <%= button_tag 'follow', class: 'btn btn-success' %>
                  <% end %>
                <% end %>
              </th>
            </tr>
          </table>
      <p>
      </div>
    </div>
  </div>
  <% end %>
</div>

followers_controller.rb

class FollowersController < 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_controller.rb

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
    @gifts = @user.gifts
  end

  def index
    @users = User.all

    if params[:search]
     @users = User.search(params[:search]).order("created_at DESC")
    else
     @users = User.all.order("created_at DESC")
    end

  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

private
  def get_gifts
    @gifts = Gift.find(params[:id])
  end


end

的routes.rb

Rails.application.routes.draw do
  devise_for :users

  resources :users, only: [:index] do
  post :follow, :to => 'followers#create'
  delete :unfollow, :to => 'followers#destroy'
  end

  resources :gifts
  resources :wishlists

  get 'wishlists/index'
  get '/wishlists', :to => 'wishlists#index'
  get '/users/:id', :to => 'users#show', :as => :user
  get '/users', :to => 'users#index'


  root "gifts#index"
end

user.rb(model)

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

 acts_as_follower
 acts_as_followable

  has_many :gifts
  belongs_to :wishlist

  def self.search(search)
    where("name ILIKE ?", "%#{search}%")
  end
end

路线:

C:\Sites\gladlygift>rake routes
                  Prefix Verb   URI Pattern                        Controller#A
tion
        new_user_session GET    /users/sign_in(.:format)           devise/sessi
ns#new
            user_session POST   /users/sign_in(.:format)           devise/sessi
ns#create
    destroy_user_session DELETE /users/sign_out(.:format)          devise/sessi
ns#destroy
           user_password POST   /users/password(.:format)          devise/passw
rds#create
       new_user_password GET    /users/password/new(.:format)      devise/passw
rds#new
      edit_user_password GET    /users/password/edit(.:format)     devise/passw
rds#edit
                         PATCH  /users/password(.:format)          devise/passw
rds#update
                         PUT    /users/password(.:format)          devise/passw
rds#update
cancel_user_registration GET    /users/cancel(.:format)            devise/regis
rations#cancel
       user_registration POST   /users(.:format)                   devise/regis
rations#create
   new_user_registration GET    /users/sign_up(.:format)           devise/regis
rations#new
  edit_user_registration GET    /users/edit(.:format)              devise/regis
rations#edit
                         PATCH  /users(.:format)                   devise/regis
rations#update
                         PUT    /users(.:format)                   devise/regis
rations#update
                         DELETE /users(.:format)                   devise/regis
rations#destroy
             user_follow POST   /users/:user_id/follow(.:format)   followers#cr
ate
           user_unfollow DELETE /users/:user_id/unfollow(.:format) followers#de
troy
                   users GET    /users(.:format)                   users#index
                   gifts GET    /gifts(.:format)                   gifts#index
                         POST   /gifts(.:format)                   gifts#create
                new_gift GET    /gifts/new(.:format)               gifts#new
               edit_gift GET    /gifts/:id/edit(.:format)          gifts#edit
                    gift GET    /gifts/:id(.:format)               gifts#show
                         PATCH  /gifts/:id(.:format)               gifts#update
                         PUT    /gifts/:id(.:format)               gifts#update
                         DELETE /gifts/:id(.:format)               gifts#destro

               wishlists GET    /wishlists(.:format)               wishlists#in
ex
                         POST   /wishlists(.:format)               wishlists#cr
ate
            new_wishlist GET    /wishlists/new(.:format)           wishlists#ne

           edit_wishlist GET    /wishlists/:id/edit(.:format)      wishlists#ed
t
                wishlist GET    /wishlists/:id(.:format)           wishlists#sh
w
                         PATCH  /wishlists/:id(.:format)           wishlists#up
ate
                         PUT    /wishlists/:id(.:format)           wishlists#up
ate
                         DELETE /wishlists/:id(.:format)           wishlists#de
troy
         wishlists_index GET    /wishlists/index(.:format)         wishlists#in
ex
                         GET    /wishlists(.:format)               wishlists#in
ex
                    user GET    /users/:id(.:format)               users#show
                         GET    /users(.:format)                   users#index
                    root GET    /                                  gifts#index

0 个答案:

没有答案