Why is the object nil in this view? - ruby-on-rails

I want to create a new article object, but have this error:
Showing
/Users/levanngoc/code/blog_app/app/views/articles/new.html.erb where
line #3 raised:
undefined method `errors' for nil:NilClass
How can I fix it?
this is my controller:
class ArticlesController < ApplicationController
def index
#article = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
end
def edit
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
end
def destroy
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
this is my view file: new.html.erb
<h1>New Article</h1>
<%= form_with scope: :article,url: articles_path , local: true do |form| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %>
prohibited this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>

Your new view uses #article but the new action in the controller doesn't create this instance variable. You must change it to:
def new
#article = Article.new
end

You must declare:
#article = Article.new

Related

It seems my 'new.html.erb' file does not rerender

I'm trying to display errors conditionally.
Here are my two files
new.html.erb
<h1>Create a New Article</h1>
<% if #article.errors.any? %>
<h2>Please fix the following errors</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= form_with scope: :article, url: articles_path, local: true do |f| %>
<p>
<%= f.label :title %> <br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %> <br/>
<%= f.text_area :description %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
and articles_controller.rb
class ArticlesController < ApplicationController
def show
#article = Article.find(params[:id])
end
def index
#articles = Article.all
end
def new
#article = Article.new
end
def create
#article = Article.new(params.require(:article).permit(:title, :description))
if #article.save
redirect_to #article
else
puts #article.errors.any?
render 'new'
end
end
end
In my def create action I want to rerender my new.html.erb which should display errors. For some reason I don't think it rerenders.
Thanks for your help
So I figured it out with help from some people on Udemy. In Rails 7 we have to add status: :unprocessable_entity after the render new like so
def create
#article = Article.new(params.require(:article).permit(:title, :description))
if #article.save
redirect_to #article
else
puts #article.errors.any?
render 'new', status: :unprocessable_entity
end
end

Error not displaying with form_with

Good Morning.
I'm following the tutorial http://edgeguides.rubyonrails.org/getting_started.html and I find the following:
I have a controller called ArticlesController. The create method uses the "if # article.save" statement to save the #article object, and if something goes wrong then render 'new'. Similarly the update method uses "if #article.update (article_params)" to update the record for that article, and if something goes wrong render 'edit'.
In new and edit views <% if # article.errors.any? %> is used to determine if there was any error and display the corresponding message, but the problem is that in the new view works fine, but in the edit view #article.errors.any view? returns false even if there is an error.
Can someone tell me what's wrong. Thank you very much.
Then I put the ArticlesController class, the views new.html.erb and edit.html.erb, and the article model.
class ArticlesController < ApplicationController
def new
#article = Article.new
end
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
def index
#articles = Article.all
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
------------------------------------------------------------------
<h1>New Article</h1>
<%= form_with scope: :article, url: articles_path, local: true do |form| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
----------------------------------------------------------------------
<h1>Edit article</h1>
<%= form_with(model: #article) do |form| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
---------------------------------------------------------------------
class Article < ApplicationRecord
validates :title, presence: true,
length: { minimum: 5 }
validates :text, presence: true
end
All forms generated by form_with will be submitted by an XHR (Ajax) request by default. if you want to disable remote forms then you can do it with local like you did in the form in new.html.erb.
change this in your edit.html.erb :
<%= form_with model: #article, local: true do |form| %>
It's a limitation of Turbolinks, which does not work with render
See here https://joey.io/turbolinks-rendering-form-errors/ and here https://github.com/jorgemanrubia/turbolinks_render
Got the same error today. They are using render with form_with in their documentation but it doesn't work.
I used form_for and it fixed the problem.
<%= form_for article do |form| %>

First argument in form cannot contain nil or be empty (rails 5)

I've been following the rails blog tutorial (you know, that one ) and i've come to a point where every time i reference #articles on the update form, rails takes it as a nil, it says:
First argument in form cannot contain nil or be empty
Here's the form
<h1>Edit article</h1>
<%= form_for #article do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
And the controller for the articles:
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
def show
#article = Article.find(params[:id])
end
def index
#articles = Article.all
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
It does not look like you have an edit method in your Articles controller.
def edit
#article = Article.find(params[:id])
end
Just so it is clear. The edit method is what is called with the GET route that shows the form. Update is the PATCH/PUT route that takes the form in and updates the record. So, the form is shown by the edit method via GET, and processed by the update method via PUT/PATCH.

Rails: How to pass instance variables from a controller to render partial

I am having trouble passing an instance variable (#article) from a controller (articles_controller.rb) to a partial render (_form.html.erb) in Ruby.
Here is the error from being sent back:
`undefined method `errors' for nil:NilClass`
articles_controller.rb:
class ArticlesController < ApplicationController
def new
end
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', :article => #article
end
end
def show
#article = Article.find(params[:id])
end
def index
#articles = Article.all
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
new.html.erb
<h1>New Article</h1>
<%= form_for :article, url: articles_path do |f|%>
<%= render partial: "form", :locals => {:article => #article} %>
<% end %>
<% link_to 'Back', articles_path %>
_form.html.erb
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited this
article from being saved:
</h2>
<ul>
<% #article.errors.full_messeages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br />
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
Any help would be appreciated
def new
#article = Article.new
end
<%= render partial: "form", collection: #article %>
or even
<%= form_for #article do |f|%>
<%= render 'form' %>
<% end %>
depends on your needs
I had this challenge when working on a Rails 6 application.
I wanted to use an instance variable in a partial (app/views/shared/_header.html.erb) that was defined in a different controller (app/controllers/categories_controller.rb).
Here's how I did it:
The instance variable that I wanted to use is #categories which is defined as:
# app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def index
#categories = Category.all
end
.
.
.
end
Firstly, I rendered the app/views/shared/_header.html.erb partial in the app/views/layouts/application.html.erb and passed the #categories instance variable into it this way:
<%= render partial: '/shared/header', locals: { categories: #categories } %>
And then I used the instance variable in the partial this way:
# app/views/shared/_header.html.erb
<% #categories.each do |category| %>
<%= link_to category do %>
<%= category.name %>
<% end %>
<% end %>
However, this will require a controller action that sets a #categories instance variable for every controller views that will use the partial.
If you want to make variables globally available in your controllers and views, this could help: Rails: Set a common or global instance variable across several controller actions
That's all.
I hope this helps
You should do like this, removing #. You are passing it to local variable article so you could access it with article not #article:
<% if article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(article.errors.count, "error") %> prohibited this
article from being saved:
</h2>
<ul>
<% article.errors.full_messeages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br />
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
For more info, please take a look at passing-local-variables

Getting undefined method `errors' for nil:NilClass

I'm doing the Ruby on Rails tutorial at http://guides.rubyonrails.org/getting_started.html
However when I get to "5.11 Adding Some Validation", after adding the error message to display when I try to create a new post from the index view I get
"undefined method `errors' for nil:NilClass"
This is the error line highlighted.
I have already created a new Post in my new action in the controller.
Here is the source
new.html.erb
<h1>New Post</h1>
<%= form_for :post, url: posts_path do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
posts_controller.rb
class PostsController < ApplicationController
def new
#Post = Post.new
end
def create
#post = Post.new(post_params)
if #post.save
redirect_to #post
else
render 'new'
end
end
def show
#post= Post.find(params[:id])
end
def index
#posts = Post.all
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(post_params)
redirect_to #post
else
render 'edit'
end
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Thanks.
Change
#Post = Post.new
To
#post = Post.new
Subtle but deadly
You've got error in your controller:
def new
#Post = Post.new
end
just change #Post to #post.

Resources