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?
Related
I am getting this error when I create my articles:
error: NameError in ArticlesController#create
undefined local variable or method `article_params' for # Did you mean? article_path
image of error :
my code :
class ArticlesController < ApplicationController
def new
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
flash[:notice] = "Article was submitted succsefully"
redirect_to (#article)
else
render :new
end
private
def article_params
params.require(:article).permit(:title, :description)
end
end
end
put article params outside of create
class ArticlesController < ApplicationController
def new
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
flash[:notice] = "Article was submitted succsefully"
redirect_to (#article)
else
render :new
end
end
# this is show method
def show
#article = Article.find(params[:id])
end
private
def article_params
params.require(:article).permit(:title, :description)
end
end
I think the create action was not properly closed,therefore you have the article_params method inside the create action,delete the 'end' at the last line and add an 'end' to the create action,its a syntax error. like this
class ArticlesController < ApplicationController
def new
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
flash[:notice] = "Article was submitted succsefully"
redirect_to (#article)
else
render :new
end
end
private
def article_params
params.require(:article).permit(:title, :description)
end
end
I am very new to Ruby on Rails. I am attempting to go through the guide and learn by making the simple blog. When trying to access the localhost is gives me the error in the title. I'm sure this is an easy fix, I'm just unable to see it at the moment. Thank you!
class ArticlesController < ApplicationController
def new
#article = Article.new
end
def create
#article = Article.new(article.params)
if #article.save
redirect_to #article
else
render 'new'
end
private
def article_params
params.require(:article).permit(:title, :text)
end
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
end
Indentation
If your text editor cannot automatically indent code, use another one!
If your text editor can indent code, please use it ;)
class ArticlesController < ApplicationController
def new
#article = Article.new
end
def create
#article = Article.new(article.params)
if #article.save
redirect_to #article
else
render 'new'
end
private
def article_params
params.require(:article).permit(:title, :text)
end
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
end
You can see that def create is the last method definition with the correct indentation, so the problem must come from here.
Params
You define article_params method but call article.params. That is likely another problem.
Private methods
Any method that is defined after private keyword is private. Not just article_params but also show and index, in your case. I guess the last two should be public (i.e. above private keyword).
Add end word in your create action. That must work
class ArticlesController < ApplicationController
def new
#article = Article.new
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
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
end
Can someone tell me why id and category_id keep getting switched in Rails?? im losing my mind! the console says its correctly but when i get an error it shows that params switches it around. This my controller below:
class ArticlesController < ApplicationController
def new
#article = Article.new
#category = Category.find(params[:category_id])
end
def create
#article = Article.new(articles_params)
#category = Category.find(params[:category_id])
# binding.pry
#article.category_id = #category.id
binding.pry
if #article.save
redirect_to category_path(#category)
else
render :new
end
end
def edit
#category = Category.find(params[:category_id])
#article = Article.find(params[:id])
end
def update
#article = Article.find(params[:id])
#category = Category.find(params[:category_id])
if #article.update(articles_params)
redirect_to category_path(#category)
else
render :edit
end
end
def destroy
# binding.pry
#article = Article.find(params[:id])
#article.destroy
redirect_to category_path(#category)
end
private
def articles_params
params.require(:article).permit(:title, :body)
end
end
Everything looks fine.
Look below on the request parameters from your screenshot:
{
"id" : 1,
"category_id" : 2
}
You are looking for id 1 in the context of your Article controller.
1 refers to the article with id 1.
Meanwhile you are looking for a Category with id 2 (category_id 2).
Category.find(params[:category_id])
The error is saying that it cannot find a Category object with id 2.
The 2 comes from your request. If something is wrong, it's in your request creation.
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.
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