Error "param is missing or the value is empty: article" - ruby-on-rails

I want to create an instance of "article" and the browser gives me an error
param is missing or the value is empty: article
Here my article controller
class ArticlesController < ApplicationController
def index
#article = Article.all
end
def new
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def show
#article = Article.find(params[:id])
end
private
def article_params
params.require(:article).permit(:title, :body)
end
end
how fix?

This error indicates that the article parameter is missing or empty in the new form. Go into the project/views/new.html.erb and make sure that you are actually passing :article to the url: articles_path.
<%= form_for :*article*, url: articles_path do |f| %>
I had a typo in article (accidentally had typed articles) which produced exactly the same error.

This is the method that raises the error.
def article_params
params.require(:article).permit(:title, :body)
end
It means you are not passing such parameter to the controller. Make sure you have a form in the new/edit view and you are properly passing its values.

Try this code, it worked for me:
def article_params
params.require(:articles).permit(:title, :body)
end

Related

articles_controller.rb:26: syntax error, unexpected end-of-input, expecting keyword_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

friendly_id creates new entry on UPDATE

I'm using friendly_id 5.1.0, and when I try to update an entry, for exemple creating a new Article, instead of updating the data of the entry, it creates a new one.
I have slugged the title and when I don't change it when editing an article, it creates a slug with a bunch of numbers/letters :
http://localhost:3000/articles/first-article-2d392b8e-92b8-44b0-ad67-50dd674f7aaa
Here's my article.rb Model :
class Article < ActiveRecord::Base
extend FriendlyId
has_many :comments
friendly_id :title, :use => :slugged
validates :title, presence: true,
length: { minimum: 5}
def should_generate_new_friendly_id?
new_record? || title_changed?
end
when I add :use => [:slugged, :history], when I update an entry and keep the same title, it can't save it because my :slug field is unique :true.
Here's my articles_controller.rb :
class ArticlesController < ApplicationController
def index
#articles = Article.all.order(created_at: :desc)
end
def show
#article = Article.friendly.find(params[:id])
if request.path != article_path(#article)
redirect_to #article, status: :moved_permanently
end
end
def new
#article = Article.new
end
def edit
#article = Article.friendly.find(params[:id])
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.friendly.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.friendly.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
Here's my GitHub repository whith my (unfinished) project : https://github.com/TheDoctor314/blog
This issue has nothing to do with FriendlyID.
Your problem is here (a form used on both new and edit):
<%= bootstrap_form_for :article, url: articles_path do |f| %>
It does not attempt to use your #article object to built that form. So your form always issues a POST request to articles_path, which results in create every time. What you should do instead is:
<%= bootstrap_form_for #article do |f| %>
That way form builder will check if that object is persisted? already, and if so, generate a form that issues a PATCH request to that specific article that triggers an update action. It will try to guess the URL by itself. And it will succeeed only if you follow conventions tightly enough.
If #article is not persisted?, it will do what it did: make a POST to articles_path.
Permit id in params
params.require(:article).permit(:id, :title, :text)
Hope that helps!
The Edit form is routing to create action for articles controller instead of update action. You need to change your form path, when editing the files.
If you see the articles index action, you can see new articles are being added, instead of updating

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

I am getting this error message after submitting a test post: param is missing or the value is empty: content?

I am trying to submit a test post through my form, and I am getting this error: param is missing or the value is empty: content. I am requiring "content" and permitting a "title". Both fields were filled out when submitting the post or "thought" in my app. I believe the problem has something to do with strong parameters. I can't find the answer anywhere else on the internet. Here is my controller.
class ThoughtsController < ApplicationController
def index
end
def new
#thought = Thought.new(params[:id])
end
def create
#thought = Thought.new(params[post_params])
#thought.save
if #thought.save
redirect_to #thought
else
render :new
end
end
private
def post_params
params.require(:content).permit(:title)
end
end
Thanks for the help.
The following should work. You propably understand the strong_parameters a bit wrong. If your thought object has :content and :title attributes, they should be listed in permit parenthesis - this will mean you allow their mass-assignment.
def create
#thought = Thought.new(post_params)
if #thought.save
redirect_to #post
else
render :new
end
end
private
def post_params
params.require(:thought).permit(:content, :title)
end

Rails - undefined method `title'

I'm trying to learn some Rails basics with Getting Started with Rails, but I can't seem to get pass an error:
undefined method `title' for nil:NilClass
Showing /blog/app/views/articles/show.html.erb where line #3 ...
I have Ruby 1.9.3, and Rails 4.1.0.
My views/articles/show.html.erb looks like this:
<p>
<strong>Title:</strong>
<%= #article.title %> # line #3
</p>
<p>
<strong>Text:</strong>
<%= #article.text %>
</p>
My controllers/articles_controller.rb looks like that:
class ArticlesController < ApplicationController
def index
end
def create
#article = Article.new(article_params)
#article.save
redirect_to #article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
def new
end
def show
#article = Article.find(params[:id])
end
end
The error pops out whenever I try to create a new post through /articles/new
The main issue you're facing is that your new & show methods are private since they come after the 'private' declaration within your class.
Move the code for new & show above the private declaration within your class and you should get things functioning. Also, pass article params into the find for show, since you're using Strong Parameters.
class ArticlesController < ApplicationController
def index
end
def create
#article = Article.new(article_params)
#article.save
redirect_to #article
end
def new
end
def show
#article = Article.find(article_params)
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
Check if the article is created - since it is nil:NilClass I suppose note.
Try to add the following in new method:
def new
#article = Article.new
end
If this still doesn't solve it, please post the new view & server output when saving the item.

Resources