ActiveRecord :: RecordNotFound,无法找到带有'id'= 14的Empower

时间:2015-02-27 17:53:03

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

虽然有些问题与我面临的问题非常接近,但解决方案并不适用于我。像这样Couldn't find User with id= (ActiveRecord::RecordNotFound)

我正在采取行动之前找到像这样的应用程序

before_action :find_empower, only: [:show, :edit, :update, :destroy]

这是我在控制器中的 destroy 方法:

def destroy
    @empower.destroy
    respond_to do |format|
      format.html { redirect_to empowers_url, notice: 'successfully destroyed.' }
      format.json { head :no_content }
    end
  end


private

  def find_empower
    @empower = Empower.find(params[:id])
  end

但每当我删除它时都会引发异常:

ActiveRecord :: RecordNotFound

无法找到Empower,其中包含' = 14

即使我将其重定向到empowers_url,它仍然会尝试点击"empowers/14",其中14是我要删除的列表的ID。

On Rails控制台:

   > Empower.find(14)
      Empower Load (0.4ms)  SELECT  "empowers".* FROM "empowers" WHERE "empowers"."id" = ? LIMIT 1  [["id", 14]]
    ActiveRecord::RecordNotFound: Couldn't find Empower with 'id'=14
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/core.rb:154:in `find'
        from (irb):1
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
        from /home/abhinay/rails_project/iSupportWomen/bin/rails:8:in `<top (required)>'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /home/abhinay/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
        from /home/abhinay/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        from /home/abhinay/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'

Empower模型:

class Empower < ActiveRecord::Base
  acts_as_votable
  belongs_to :user

  has_many :comments

  has_many :paths
  has_many :notes

  accepts_nested_attributes_for :paths, 
                                reject_if: proc { |attributes| attributes['name'].blank? },
                                allow_destroy: true
  accepts_nested_attributes_for :notes, 
                                reject_if: proc { |attributes| attributes['step'].blank? },
                                allow_destroy: true

  validates :category, :title, :description, :image, presence: true

  has_attached_file :image,
  :storage => :dropbox,
  styles: { :medium => "300x300>"},
  :dropbox_credentials => Rails.root.join("config/dropbox_config.yml"),
  # :dropbox_options => { :path => proc { |style| "#{Rails.env}/#{style}/#{id}_#{image.original_filename}" } },
  :unique_filename => true
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
  # has_attached_file :image, styles: { :medium => "300x300>" }


end

更新

结果是路由本身的问题,我有另一个页面被调用,我已经修改了我的路径根路径到家,root "empowers#home"但是在将其更改为root "empowers#index"之后删除记录似乎正在工作再次。如果有人对此有答案,那么我真的很想知道。

1 个答案:

答案 0 :(得分:0)

你的代码很好。你的数据库中没有Empower和ID为14。如果你想在你的数据库中看到Empowers,你可以在rails控制台中做这样的事情:

Empower.all.each do |e|
  puts e.id
end

我的猜测是你成功删除了Emopower(ID为14),但是事后发生了视图或控制器错误。所以它被删除了,你修复了错误,但你试图删除同样的东西(不再是你的数据库中的那个)。