我正在构建一个应用程序(在Michael Hartl's chapter 11之后),用户可以在其中跟踪由其他用户创建的项目。
我创建了一个ProjectRelationship模型来保存两个组件:用户的follower_id和项目的projectuser_id。外键已经设置好了。
现在,我的_follow_form.html.erb
页面呈现"关注"或者"取消关注"取决于current_user
是否跟随项目。请参阅下面的代码,看看我错过了什么。
现在,在每个项目显示页面上生成关注按钮。但是,当我单击_follow.html.erb
生成的按钮跟随按钮时,由于POST没有发生,因此当我调用@project.followers.count
时,它似乎不会跟随项目或更新计数。
因此,当我点击关注按钮时,网址变得混乱。见例:
#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 =>通过迁移变更确实。
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
项目/ show.html.erb
<%= render 'follow_form' if signed_in? %>
项目/ _follow_form.html.erb
<% if current_user.following_project?(@project) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
项目/ _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 %>
项目/ _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 %>
答案 0 :(得分:0)
首先 - 如果您在控制台中运行projectfollow!(project)
和projectunfollow!(project)
(与用户,项目等),它们是否正常运行?
对于您的表单,请尝试以下操作,看看它是否有效:
<%= form_for(current_user.project_relationships.build, url: project_relationships_path(project_id: @project.id)) do |f| %>
然后在你的项目关系控制器中:
class ProjectRelationshipsController < ApplicationController
def create
@project = Project.find(params[:project_id])
current_user.projectfollow!(@project)
redirect_to @project
end
end
因此,如果您的创建网址为/project_relationships
(通过POST),则上述内容应发布到/project_relationships?project_id=5
,然后控制器就可以找到该项目。
此外,尝试重命名您的方法,使它们有意义:
def following_project?(project)
end
def follow_project!(project)
end
def unfollow_project!(project)
end
现在current_user.following_project?(project)
很有意义!
好的,我认为以下是问题所在,在create
行动中你从params获得了id:
@project = Project.find(params[:project_relationship][:projectuser_id])
但是,在您的表单中,您没有设置隐藏字段的值:
<%= f.hidden_field :projectuser_id %>
将其更改为以下内容并查看其是否有效:
<%= f.hidden_field :projectuser_id, value: @project.id %> # or wherever the id is from
答案 1 :(得分:0)
问题是我的关注/取消关注表单嵌入了导致错误的另一种形式。一旦取出,工作!