本文介绍了为什么我的用户关注/取消关注按钮不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序(遵循 Michael Hartl 的第 11 章)用户可以在这里关注其他用户创建的项目.

I am working on building an application (following Michael Hartl's chapter 11) where users can follow projects that are created by other users.

我创建了一个 ProjectRelationship 模型来保存两个组件:用户的 follower_id 和项目的 projectuser_id.外键已如此设置.

I created a ProjectRelationship model to hold two components: follower_id for the users and projectuser_id for the projects. The foreign keys have been set up as such.

现在,我的 _follow_form.html.erb 页面根据 current_user 是否关注项目呈现关注"或取消关注".请查看下面我的代码,看看我缺少什么.

Right now, my _follow_form.html.erb page renders "follow" or "unfollow" depending on whether the current_user is following the project. Please see my code below and see what I am missing.

现在,每个项目展示页面上都会生成关注按钮.但是当我点击由_follow.html.erb生成的按钮follow按钮时,当我调用@project.followers.count 因为 POST 没有发生.

Right now, the follow button is generated on each project show page. But when I click the button follow button that is generated by _follow.html.erb, it does not seem to follow the project or update the count when I call @project.followers.count as the POST is not happening.

因此,当我点击关注按钮时,URL 变得混乱.见示例:

And thus, when I click follow button, the URL becomes all jumbled. See example:

#Goes from
domain.com/projects/21

#to
domain.com/projects/21?utf8=%E2%9C%93&authenticity_token=5EQmU0EkHB5yKDYakqL78piMWzZl0CfdpHFEqBeQiN4%3D&project_relationship%5Bprojectuser_id%5D=21&commit=Follow%22

**更新:现在似乎可以工作了,但我不确定我是否真的改变了任何东西,只是通过迁移更改摆脱了 follower_id 索引 :unique => true.

schema.rb

create_table "project_relationships", :force => true do |t|
  t.integer  "follower_id"
  t.datetime "created_at",     :null => false
  t.datetime "updated_at",     :null => false
  t.integer  "projectuser_id"
end

add_index "project_relationships", ["follower_id"], :name => "index_project_relationships_on_follower_id", :unique => true
add_index "project_relationships", ["projectuser_id"], :name => "index_project_relationships_on_projectuser_id"

routes.rb

resources :projects do       
  resources :comments 
  member do
    get :following
  end   
end
resources :project_relationships, only: [:create, :destroy]

project_relationship.rb

class ProjectRelationship < ActiveRecord::Base
   attr_accessible :projectuser_id

   belongs_to :user, foreign_key: "follower_id"
   belongs_to :project, foreign_key: "projectuser_id"
end

project.rb

has_many :project_relationships, foreign_key: "projectuser_id"
has_many :favorited_by, through: :project_relationships, source: :user

user.rb

has_many :project_relationships, foreign_key: "follower_id"
has_many :followed_projects, through: :project_relationships, source: :project

def following_project?(project)
  project_relationships.find_by_follower_id(project.id)
end

def follow_project!(project)
  project_relationships.create!(projectuser_id: project.id)
end

def project_unfollow!(project)
  project_relationships.find_by_projectuser_id(project.id).destroy
end

project_relationships_controller.rb

class ProjectRelationshipsController < ApplicationController

def create
    @project = Project.find(params[:project_relationship][:projectuser_id])
    current_user.follow_project!(@project)
    redirect_to @project
  end

  def destroy
    @project = ProjectRelationship.find(params[:id]).followed_project
    current_user.project_unfollow!(@project)
    redirect_to @project
  end
end

projects/show.html.erb

<%= render 'follow_form' if signed_in? %>

projects/_follow_form.html.erb

<% if current_user.following_project?(@project) %>
    <%= render 'unfollow' %>
<% else %>
    <%= render 'follow' %>
<% end %>

projects/_follow.html.erb

<%= form_for(current_user.project_relationships.build(projectuser_id: @project.id)) do |f| %>
  <div><%= f.hidden_field :projectuser_id %></div>
  <%= f.submit "Follow", class: "btn btn-large btn-primary" %>
<% end %>

projects/_unfollow.html.erb

<%= form_for(current_user.project_relationships.find_by_projectuser_id(@project),
         html: { method: :delete }) do |f| %>
  <%= f.submit "Unfollow", class: "btn btn-large" %>
<% end %>

推荐答案

问题是我的关注/取消关注表单被嵌入到导致错误的另一个表单中.取出后,工作!

The problem was that my follow/unfollow form was embedded in another form which caused the error. Once taken out, worked!

这篇关于为什么我的用户关注/取消关注按钮不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 20:37