7

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

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

现在,我的_follow_form.html.erb页面根据是否关注current_user项目呈现“关注”或“取消关注”。我在下面附上了我的代码。

现在,在每个项目展示页面上都会生成关注按钮。在我单击生成的按钮之后_follow.html.erb,它会相应地跟随项目。

但是当我按下“取消关注”时,我得到一个错误:

NoMethodError in ProjectRelationshipsController#destroy
undefined method `unfollow_project!' for #<User:0x007f9ed83dd6b8>

Application Trace | Framework Trace | Full Trace
app/controllers/project_relationships_controller.rb:12:in `destroy'

架构.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", ["projectuser_id"], :name => "index_project_relationships_on_projectuser_id"

路线.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

项目.rb

has_many :project_relationships, foreign_key: "projectuser_id"
has_many :favorited_by, through: :project_relationships, source: :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_projectuser_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]).project
    current_user.unfollow_project!(@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 %>

项目/_followerstats.html.erb

<%= @project.favorited_by.count %>
4

3 回答 3

4

你已经定义了一个方法 project_unfollow!但是user.rb你调用current_user.unfollow_project!project_relationships_controller.rb

于 2013-04-23T22:43:23.947 回答
1

将您unfollow_project!的替换project_unfollow!为模型文件中声明的内容。

于 2013-04-23T23:42:29.257 回答
1

您调用unfollow_project!ProjectRelationshipsController#destroy但您要调用的方法是project_unfollow!在您的user.rb.

于 2013-04-23T22:44:37.733 回答