Activerecord::Recordnotfound in Articlescontroller#Show Couldn't Find Article Without an Id

ActiveRecord::RecordNotFound in ArticlesController#show Couldn't find Article without an ID

You are redirecting before actually saving your article.

This:

def create
@article = Article.new(article_params)
redirect_to @article
@article.save
end

should be:

def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end

And if you want to add some error handling as well:

def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render :new
end

ActiveRecord::RecordNotFound in ArticlesController#show Couldn't find Article with 'id'=edit

Then that's the cause of the error. 'edit' page requires an id param otherwise. as in your controller:

def edit 
@article = Article.find(params[:id])
end

It needs params[:id]

So you need to use /articles/id/edit (replace id with your actual id)

ActiveRecord::RecordNotFound in ArticlesController#show - Couldn't find Article with 'id'=13

In your destroy action, change the redirect to articles_path (plural) instead of the singular version. The singular version will attempt to look for the article that you just deleted, which of course no longer exist.

def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path # pluralized the path
end

How to solve ActiveRecord::RecordNotFound?

The line is giving you an ActiveRecord::RecordNotFound exception is this one

@article = Article.find(params[:article][:id])

You have no key id in the hash within article.

Friendly_id not working version 5.3 rails 6

There is a conflict between the cancancan gem. You need to add a file called cancan.rb in config/initializers with the following

require_dependency 'cancan/model_adapters/active_record_4_adapter'

if defined?(CanCan)
class Object
def metaclass
class << self; self; end
end
end

module CanCan
module ModelAdapters
class ActiveRecord4Adapter < ActiveRecordAdapter
@@friendly_support = {}

def self.find(model_class, id)
klass =
model_class.metaclass.ancestors.include?(ActiveRecord::Associations::CollectionProxy) ?
model_class.klass : model_class
@@friendly_support[klass]||=klass.metaclass.ancestors.include?(FriendlyId)
@@friendly_support[klass] == true ? model_class.friendly.find(id) : model_class.find(id)
end
end
end
end
end


Related Topics



Leave a reply



Submit