Rails 4错误:ActiveRecord :: AssociationTypeMismatch

时间:2016-09-04 14:02:17

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord associations


嘿那里,
我是Rails的新手,我已经设法为我的项目(工具)和用户创建了一个收藏夹控制器。
我正在用户的索引正确显示所有收藏项目(工具)。在搜索视图中,我提供了收藏夹和收藏夹的链接,但是当我点击某个项目(工具)的链接时出现错误

在收藏项目时,我在浏览器中收到此错误:

ActiveRecord::AssociationTypeMismatch in ToolsController#favorite
Tool(#46153692) expected, got NilClass(#20297664)

请求参数

{"_method"=>"put",
 "authenticity_token"=>"vlWYHcp1K4Eu8WzjyEM8f6Eta9MNjgojtkr6RlG6n7121PGiWtXU8kDq9yXOfzGzw5grSc4GCqlcoK1UiLEsng==",
 "type"=>"favorite", #WhatMyUserDid
 "id"=>"1"} #MyUserId

我的目标是为用户添加收藏的项目(工具) (=>错误)显示收藏的项目(工具)在用户索引视图(正常工作)显示链接到收藏夹或取消收藏,具体取决于current_user的收藏夹 (尚未实施)

这是我的代码:

应用程序/模型/ favorite_tool.rb

class FavoriteTool < ActiveRecord::Base
    belongs_to :tool
    belongs_to :user
end

应用程序/模型/ tool.rb

class Tool < ActiveRecord::Base
    belongs_to :user

    # Favorited by users
    has_many :favorite_tools # just the 'relationships'
    has_many :favorited_by, through: :favorite_tools, source: :user # the actual users favoriting a tool


    mount_uploader :cover_filename, CoverUploader
end

应用程序/模型/ user.rb

class User < ActiveRecord::Base

  # Include default devise modules. Others available are:

  # :confirmable, :lockable, :timeoutable and :omniauthable

  devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable


  has_many :tools

  # Favorite tools of user
  has_many :favorite_tools # just the 'relationships'
  has_many :favorites, through: :favorite_tools, source: :tool # the actual tools the user favorites


  mount_uploader :avatar_filename, AvatarUploader

end

应用程序/控制器/ tools_controller.rb

class ToolsController < ApplicationController
    before_action :find_tool, only: [:show, :edit, :update, :destroy]

    # Add and remove favorite recipes
    # for current_user
    def favorite
        type = params[:type]
        if type == "favorite"
            current_user.favorites << @tool
            redirect_to :back, notice: 'You favorited #{@tool.title}'

        elsif type == "unfavorite"
            current_user.favorites.delete(@tool)
            redirect_to :back, notice: 'Unfavorited #{@tool.title}'

        else
            # Type missing, nothing happens
            redirect_to :back, notice: 'Nothing happened.'
        end
    end

    def index
        @favorites = current_user.favorites
        @tools = Tool.where(user_id: current_user).order("created_at DESC")
        @user = current_user
    end

    def search
        @tool = Tool.find(1)
        @tools = Tool.all.order("created_at DESC")
    end

    def show
    end

    def new
        @tool = current_user.tools.build
    end

    def create
        @tool = current_user.tools.build(tool_params)

        if @tool.save
            redirect_to tools_path
        else
            render 'new'
        end
    end

    def edit
    end

    def update
        if @tool.update(tool_params)
            redirect_to tools_path
        else
            render 'edit'
        end
    end

    def destroy
        @tool.destroy
        redirect_to tools_path
    end

    private

    def find_tool
        @tool = Tool.find(params[:id])
    end

    def tool_params
        params.require(:tool).permit(:title, :subtitle, :url, :cover_filename)
    end
end

应用程序/视图/工具/ index.html.haml

%h2 My Favorite Tools
- @favorites.each do |tool|
    = image_tag tool.cover_filename.url
    %h2= link_to tool.title, tool
    %p= tool.subtitle
    %p= link_to "Edit", edit_tool_path(tool)
    %p= time_ago_in_words(tool.created_at)

应用程序/视图/工具/ search.html.haml

- @tools.each do |tool|
    = image_tag tool.cover_filename.url
    %h2= link_to tool.title, tool
    %p= tool.subtitle
    %p= link_to tool.user.try(:username), '/users/'+tool.user_id.to_s
    %p= link_to "Favorite", favorite_tool_path(@tool, type: "favorite"), method: :put
    %p= link_to "Unfavorite", favorite_tool_path(@tool, type: "unfavorite"), method: :put
    %p= link_to "Edit", edit_tool_path(tool)
    %p= time_ago_in_words(tool.created_at)

应用程序/配置/ routes.rb中

resources :tools do
  put :favorite, on: :member
end

我希望提供的数据足够,如果没有请告诉我。我很感谢你的回复。

1 个答案:

答案 0 :(得分:1)

由于您未在before_action行动中定义:favorite,因此@tool需要加入favorite