Ruby-on-rails Rails - NoMethodError 未定义的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16180311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:51:35  来源:igfitidea点击:

Rails - NoMethodError undefined method

ruby-on-railsnomethoderror

提问by spl

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

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

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.

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

Right now, my _follow_form.html.erbpage renders "follow" or "unfollow" depending on whether the current_useris following the project. I have attached my code below.

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

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.

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

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

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

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

项目/show.html.erb

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

projects/_follow_form.html.erb

项目/_follow_form.html.erb

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

projects/_follow.html.erb

项目/_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

项目/_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

项目/_followerstats.html.erb

<%= @project.favorited_by.count %>

采纳答案by Ian Kenney

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

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

回答by tessi

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

你打电话unfollow_project!ProjectRelationshipsController#destroy,但你要调用的方法project_unfollow!是在你的定义user.rb

回答by MichaelScaria

Replace your unfollow_project!with project_unfollow!as declared in your model file.

更换你unfollow_project!project_unfollow!在你的模型文件中声明。