0

新しい管理者を作成するためにフォームを送信すると、次のエラーが表示されます。

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
4

2 に答える 2

0

管理モデルには何らかのコールバックがあります。つまり、before_save、after_save、after_validations です。管理者に電話をかけようとしているようなものですか?

admin.rb ファイルとエラーのスタック トレースを投稿すると、簡単に見つけることができます。

于 2013-10-17T19:42:21.847 に答える