【问题标题】:Rails undefined method `email' in method update方法更新中的Rails未定义方法“电子邮件”
【发布时间】:2014-07-30 01:41:43
【问题描述】:

我有想要在控制器 ProduitsController 中更新的模型 Produit。 当我尝试更新时,出现以下错误: -ProduitsController#update 中的 NoMethodError -# 的未定义方法“电子邮件”

我没有此模型(ProduitsController)的属性电子邮件,我不知道为什么更新方法调用方法电子邮件??? 有人可以帮助我或给我任何信息吗?

----------here the line the error in file produits_controllers.rb-----------------------

         ##   require 'debugger'; debugger
            respond_to do |format|
    error linge ==>if @produit.update(produit_params)
                format.html { redirect_to @produit, notice: 'produit was successfully updated.' }
                format.json { render :show, status: :ok, location: @produit }
              else 
----------------------------------------------------------------------------------------

控制器/produits_controllers.rb

  class ProduitsController < ApplicationController
          before_action :set_produit, only: [:show, :edit, :update, :destroy]

      # GET /totos
      # GET /totos.json
      def index
        @produits = Produit.all
      end

      # GET /totos/1
      # GET /totos/1.json
      def show
      end

      # GET /totos/new
      def new
        @produit = Produit.new
        @etatproduits = Etatproduit.all
        @magasins = Magasin.all
      end

      # GET /totos/1/edit
      def edit
        @etatproduits = Etatproduit.all
        @magasins = Magasin.all
      end

      # POST /totos
      # POST /totos.json
      def create
         @produit = Produit.new(produit_params)

         respond_to do |format|
           if @produit.save
             format.html { redirect_to @produit, notice: 'produit was successfully created.' }
             format.json { render :show, status: :created, location: @produit }
           else
             format.html { render :new }
             format.json { render json: @produit.errors, status: :unprocessable_entity }
           end
        end
      end

      # PATCH/PUT /totos/1
      # PATCH/PUT /totos/1.json
      def update
     ##   require 'debugger'; debugger
        respond_to do |format|
          if @produit.update(produit_params)
            format.html { redirect_to @produit, notice: 'produit was successfully updated.' }
            format.json { render :show, status: :ok, location: @produit }
          else
            format.html { render :edit }
            format.json { render json: @produit.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /totos/1
      # DELETE /totos/1.json
      def destroy
        @produit.destroy
        respond_to do |format|
         format.html { redirect_to produits_url, notice: 'produit was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

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

        # Never trust parameters from the scary internet, only allow the white list through.
        def produit_params
           params.require(:produit).permit(:nom, :etatproduit_id, :last_change_stat, :magasin_id)
        end
    end

models/produit.rb

class Produit < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, 
         :recoverable, :rememberable, :trackable, :validatable
    belongs_to :magasin
    belongs_to :etatproduit
end

migrate/20140629085947_create_produits.rb

   class CreateProduits < ActiveRecord::Migration
      def change
        create_table :produits do |t|
          t.string :nom
          t.datetime :last_change_stat

          t.timestamps
          t.belongs_to :etatproduit
          t.belongs_to :magasin
        end

        add_index :produits, :nom
        add_index :produits, :etatproduit_id, unique: true
      end
    end

日志错误

Started PATCH "/produits/1" for 192.168.56.1 at 2014-07-30 01:11:18 +0000
Processing by ProduitsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"TNgQiTyu2tVnah42G9CcfupzxV9pfCGYVKo9ehWYzXA=", "produit"=>{"nom"=>"dfbdfbfdfffd", "etatproduit_id"=>"2", "magasin_id"=>"3"}, "commit"=>"Save", "id"=>"1"}
  Produit Load (0.3ms)  SELECT  `produits`.* FROM `produits`  WHERE `produits`.`id` = 1 LIMIT 1
   (0.1ms)  BEGIN
   (0.2ms)  ROLLBACK
Completed 500 Internal Server Error in 6ms

NoMethodError (undefined method `email' for #<Produit:0x000000097a6ff8>):
  app/controllers/produits_controller.rb:49:in `block in update'
  app/controllers/produits_controller.rb:48:in `update'


  Rendered /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.4ms)
  Rendered /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
  Rendered /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.8ms)
  Rendered /home/vagrant/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (19.6ms)

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 methods undefined


    【解决方案1】:

    设计

    进一步Jaugar的回答,错误几乎肯定是Devise的结果

    您已将Devise 包含在您的Produit 模型中:

    #app/models/produit.rb
    class Produit < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, 
             :recoverable, :rememberable, :trackable, :validatable
    

    如前所述,我不知道为什么你在这个模型中使用了 Devise,但如果我们认为你想要它,你必须确保你有 Devise 需要的附带属性(:email 是一个)

    对此的立即修复(hack)是使用attr_accessoremail和其他Devise属性创建一个“虚拟”属性:

    #app/models/produit.rb
    class Produit < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, 
             :recoverable, :rememberable, :trackable, :validatable
    
      attr_accessor :email, :other, :devise, :attributes
    

    这不会解决问题 - 只是掩盖它。

    --

    解决方案

    解决方案是将所需的属性添加到您的表中,或者从您的 Produit 模型中删除 Devise

    考虑到,正如 Jaugar 建议的那样,Devise 最好在 User 模型上使用 - 我会强烈建议将其从您的 Produit 模型中删除。如果您想为您的应用程序创建authentication 功能,您最好创建一个User 模型来安装Devise

    【讨论】:

    • Tx 为您提供帮助。我的模型中有删除设备属性,它可以工作。 ^^
    【解决方案2】:

    为什么要将设计添加到产品模型中?

    设计通常添加到用户模型中,因此用户模型需要电子邮件属性。

    您应该阅读更多有关设计的文档。

    如果你真的想在产品模型中添加设计,你可以像这样添加电子邮件属性:

    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable, :timeoutable,
             :recoverable, :rememberable, :trackable, :validatable
    
      # Setup accessible (or protected) attributes for your model
      attr_accessible :email, :password, :password_confirmation, :remember_me, :user_name
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多