@ admin.save的未定义方法

时间:2013-10-17 19:30:05

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我有一个非常奇怪的错误,当我提交表单以创建新的管理员时,我收到此错误:

NoMethodError in AdminsController#create
undefined method `admin?' for #<Admin:0x6c7f098>

排队:

if @admin.save

这个错误是如何产生的?我主要使用脚手架代码,我只删除控制器的视图!这是我的控制员:我感谢大家的帮助!

class AdminsController < ApplicationController
  before_action :set_admin, only: [ :edit, :update, :destroy]



  # GET /admins/new
  def new
    @admin = Admin.new
  end

  # GET /admins/1/edit
  def edit
  end

  # POST /admins
  # POST /admins.json
  def create
    @admin = Admin.new(admin_params)

    respond_to do |format|
      if @admin.save
        format.html { redirect_to adminpage_index_path, notice: 'Admin was successfully created.' }
      else
        redirect_to adminpage_index_path
      end
    end
  end

  # PATCH/PUT /admins/1
  # PATCH/PUT /admins/1.json
  def update
    respond_to do |format|
      if @admin.update(admin_params)
        format.html { redirect_to adminpage_index_path , notice: 'Admin was successfully updated.' }
        format.json { head :no_content }
      else
        redirect_to adminpage_index_path
      end
    end
  end

  # DELETE /admins/1
  # DELETE /admins/1.json
  def destroy
    @admin.destroy
    respond_to do |format|
      format.html { redirect_to adminpage_index_path }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_admin
      @admin = Admin.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def admin_params
      params.require(:admin).permit(:username, :vorname, :nachname, :strasse, :ort, :plz, :telefon, :handy, :email, :password, :password_confirmation)
    end
end

2 个答案:

答案 0 :(得分:2)

从我所看到的,您的Admin模型可能不会从ActiveRecord :: Base继承。确保它确实如此。该模型应如下所示:

class Admin < ActiveRecord::Base
  # ...
end

答案 1 :(得分:0)

您的管理模型有某种回调。即before_save,after_save,after_validations。那样的东西试图打电话给管理员?

发布admin.rb文件和错误的堆栈跟踪,这应该很容易找到。