ForbiddenAttributesError with Active Admin - ruby-on-rails

I 've just started working on ROR.
I made blog app following strictly ROR official doc.
It worked fine for CRDU.
Now I added Active Admin to it, it works fine on delete but gives error while creatiing/updating focusing on
raise ActiveModel::ForbiddenAttributesError
def sanitize_for_mass_assignment(attributes)
if attributes.respond_to?(:permitted?) && !attributes.permitted?
**raise ActiveModel::ForbiddenAttributesError**
else
attributes
end
In Controller, I'm using following code:
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text, :AuthorAge)
end

I think you didn't add permit_params in your active admin file.
# app/admin/xyz.rb
permit_params :comma separated attributes.
Look into this link for more detail.

Related

UnknownAttributeError on Rails website

I'm following a tutorial on how to create a Ruby-on-Rails blogging website with comments and tags, and have put my work so far on https://github.com/khpeek/jumpstart-blogger/.
The problem is that when I try to create a new article with tags as shown below,
I get an error message "ActiveRecord::UnknownAttributeError in ArticlesController#create" (see below).
However, according to the tutorial I should at this point expect the article to "go through". The articles_controller.rb has a 'set' method for tag_list:
class ArticlesController < ApplicationController
include ArticlesHelper
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
#comment = Comment.new
#comment.article_id = #article_id
end
def new
#article = Article.new
end
def tag_list=(tags_string)
end
def create
# fail
#article = Article.new(article_params)
#article.save
flash.notice = "Article '#{#article.title}' created."
redirect_to article_path(#article)
end
def destroy
#article = Article.find(params[:id])
#article.destroy
flash.notice = "Artice '#{#article.title}' deleted."
redirect_to articles_path
end
def edit
#article = Article.find(params[:id])
end
def update
#article = Article.find(params[:id])
#article.update(article_params)
flash.notice = "Article '#{#article.title}' updated."
redirect_to article_path(#article)
end
end
Further, the "to_s" method for the "Tag" class has been modified to return its name:
class Tag < ActiveRecord::Base
has_many :taggings
has_many :articles, through: :taggings
def to_s
name
end
end
In short, I don't understand why tag_list is not recognized as an attribute for Article. How might I fix this?
In you article.rb add:
def tag_list=(tags_string)
# first split the tags based on "," which is coming from the form
tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
# search if any particular tag is present or not, based on that assign them
new_tags = tag_names.collect { |name| Tag.find_or_create_by(name: name) }
self.tags = new_tags
end
Also, in articlesController.rb add:
def article_params
params.require(:article).permit(:title, :body, :tag_list)
end
Hope it helps!
It seems you are using Rails 4. Have you included tag_list into your Strong parameter permit list?

ActiveRecord::RecordNotFound in ArticlesController#edit

I am getting an error when trying to edit an Article. Here is my error:
ActiveRecord::RecordNotFound in ArticlesController#edit
articles.controller.rb
class ArticlesController < ApplicationController
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
end
def create
#article = Article.new(article_params)
if #article.save
flash[:notice] = "Article was successfully created"
redirect_to article_path(#article)
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update
flash[:notice] = "Article was successfully updated"
redirect_to article_path(#article)
else
render 'edit'
end
end
def show
#article = Article.find(params[:id])
end
You will need to pass the #article into the url. You should have a url similar to this:
<%= link_to "Edit Article", edit_article_path(#article) %>
Also be sure to check your database and make sure that an Article is actually saving. I don't see your article_params but I assume they are defined as you are using them in your create method.
Your routes.rb looks something like this:
Rails.application.routes.draw do
resources :articles
end

value always the same, different in database Rails

This is my ArticleController
def new
#article = Article.new
end
def create
#render plain: params[:article].inspect
#article = Article.new(super_params)
if #article.save
flash[:notice] = "Article has been created"
redirect_to article_path(#article)
else
render 'new'
end
end
def super_params
params.require(:article).permit(:title, :description)
end
def show
#article = Article.find_by( params[:id] )
end
The main problem is that whenever i do: articles/2 or 5 or 6 i get the same article which was created as first. In database records are good i have only this problem with render it.
I know, i did:
find_by
Instead of
find
Sory for trouble...

Rails: param is missing or the value is empty: article

I am new with Rails and I began to make a web app following the rubyonrails.org tutorial.
My app is a blog with articles.. I implemented create and edit functions which worked pretty well but suddenly an error while trying to access http://localhost:3000/articles/2/edit in order to edit an article.
The error is ActionController::ParameterMissing in ArticlesController#edit param is missing or the value is empty: articles
Here is my ruby code:
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def show
#article = Article.find(params[:id])
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
The line targeted by the error alert is params.require(:articles).permit(:title, :text)
I really don't know where the error can be because everything was ok 2 minutes ago...
Thank you for your help
You are trying to update the article in the edit method. So when you navigate to "articles/2/edit/" it tries to update the article 2. But you did not pass any params.
I think what you probably want is:
def edit
#article = Article.find(params[:id])
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
I know is late, but i hope this solution help someone. Add to ArticleController these two methods:
def edit
#article = Article.find(params[:id])
end
and
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end

Can't modify frozen hash

I keep getting the above error everytime i try to run this code below. I am trying to delete information from a form. can you look at the "destroy" method?
class ArticlesController < ApplicationController
def show
#article = Article.find(params[:id])
end
def new
#article = Article.new
end
def create
#article = Article.new(params[:article])
#article.save
redirect_to article_path(#article)
end
def destroy
#article = Article.new(params[:article])
#article.delete
#article.save
redirect_to article_path(#article)
end
def edit
#article = Article.find(params[:id])
end
end
You cannot update or save a model after it has been deleted or destroyed. Just remove the #article.save line.
Also, in your destroy method, why would you create a new instance of Article only to delete it on the next line? Your destroy method should have only this
def destroy
#article.delete
redirect_to article_path(#article)
end
You could also define the destroy method in the model instead of the controller and simply say
def destroy
self.delete
end
I experienced the can't modify frozen hash issue and this is the workaround/hack I used to fix it. This is a workaround not final solution.
Drop the table:
- From rails console: ActiveRecord::Migration.drop_table(:table_name)
Increment the model file number by 1, rename the file:
- db/migrate/1234_create_table_name.rb -> 1235_create_table_name.rb
rake db:migrate

Resources