控制器中未定义的方法错误

时间:2018-01-24 06:45:26

标签: ruby-on-rails

models / collaborators.rb:

class Collaborator < ActiveRecord::Base
  belongs_to :user
  belongs_to :wiki

  def wiki_collaborations
  end
end

控制器/ wikis_controller.rb:

class WikisController < ApplicationController
  def index
    @user = current_user
    if @user.admin?
      @wikis = Wiki.all
    elsif @user.premium?
      @wikis = Wiki.where(private: false) | @user.wiki_collaborations | @user.wikis
    elsif @user.standard?
      @wikis = Wiki.where(private: false) | @user.wiki_collaborations
    else
      @wikis = Wiki.where(private: false)
    end
  end

  def show
    @wiki = Wiki.find(params[:id])
    authorize @wiki
  end

  def new
    @wiki = Wiki.new
  end

  def create
    @wiki = current_user.wikis.new(wiki_params)
    if @wiki.save
      flash[:notice] = "Wiki was saved."
      redirect_to @wiki
    else
      flash.now[:alert] = "There was an error saving the wiki. Please try again."
      render :new
    end
  end

  def edit
    @user = current_user
    @wiki = Wiki.find(params[:id])
    @user_emails = User.where.not(id: current_user.id || @wiki.users.pluck(:id)).map(&:email)
    authorize @wiki
  end

  def update
    @wiki = Wiki.find(params[:id])
    authorize @wiki

    if @wiki.update(wiki_params)
      flash[:notice] = "Wiki was updated."
      redirect_to @wiki
    else
      flash.now[:alert] = "There was an error saving the wiki page. Please try again."
      render :edit
    end
  end

  def destroy
    @wiki = Wiki.find(params[:id])

    if @wiki.destroy
      flash[:notice] = "\"#{@wiki.title}\" was deleted successfully."
      redirect_to wikis_path
    else
      flash.now[:alert] = "There was an error deleting the wiki page."
      render :show
    end
  end

  private
  def wiki_params
    params.require(:wiki).permit(:title, :body, :private)
  end
end

我尝试访问http://localhost:3000/wikis

我收到以下错误。

error message

1 个答案:

答案 0 :(得分:0)

可能您没有为用户定义wiki_collaborations方法。

您可以delegate将其User改为

class User < ActiveRecord::Base
  delegate :wiki_collaborations, to: :collaborator
end

顺便说一句,请注意|||不同。