Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - NoMethodError undefined method

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

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.

Right now, my _follow_form.html.erb page renders "follow" or "unfollow" depending on whether the current_user is following the project. I have attached my code below.

Right now, the follow button is generated on each project show page. After I click the button follow that is generated by _follow.html.erb, it follows the project accordingly.

But when I press "unfollow" I get an error:

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'

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", ["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_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

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 %>

projects/_followerstats.html.erb

<%= @project.favorited_by.count %>
like image 242
spl Avatar asked Apr 23 '13 22:04

spl


2 Answers

you have defined a method project_unfollow! in user.rb but you call current_user.unfollow_project! in project_relationships_controller.rb

like image 192
Ian Kenney Avatar answered Oct 03 '22 18:10

Ian Kenney


You call unfollow_project! in ProjectRelationshipsController#destroy, but the method you want to call is project_unfollow! as defined in your user.rb.

like image 38
tessi Avatar answered Oct 03 '22 17:10

tessi