Rails - undefined method `title' - ruby-on-rails

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.

Related

i am trying to show my posts title on show page but it shows this error again and again . i am beginner in rails

**I made a controller and model for post and not able to show my post's
title on the show page but repeatedly i am getting the same error **
my posts_controller code :
class PostsController < ApplicationController
def index
end
def new
end
def create
#render plain: params[:post][:body].inspect
#post = Post.new(post_params)
if #post.save
redirect_to posts_path
else
render "new"
end
end
def show
#post= Post.find(:id=>params[:id])
# #article = "prateek"
end
private
def post_params
params.require(:post).permit(:title,:body)
end
end
my show.html.erb file :
<h1><%= #post.title %></h1>
my post.rb file:
class Post < ApplicationRecord
end
**I expect the result but I didn't get anything right
error = NoMethodError in Posts#show
undefined method `title' for nil:NilClass**
You're passing an hash to find, while you're suppose to pass just the id.
#post= Post.find(params[:id])
or use find_by if you want to pass an hash
#post= Post.find_by(:id => params[:id])

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

I am getting an error says that undefined local variable or method 'blog_params' (Rails)

So I got a very basic blog app running with three links to a blog post. However, when I click on a post of my selection and edit the post and click on "update blog", I will get an error saying NameError in BlogsController#update and undefined local variable or method 'blog_params' for blogscontroller. I cannot figure out what the issue is so I would like some help to guide me through
This is what my blogs controller file looks like
class BlogsController < ApplicationController
def index
#blogs = Blog.all
end
def show
#blog = Blog.find(params[:id])
end
def new
#blog = Blog.new
end
def create
#blog = Blog.new
#blog.title = params[:blog][:title]
#blog.body = params[:blog][:body]
#blog.save
redirect_to blog_path(#blog)
end
def destroy
#blog = Blog.find(params[:id])
#blog.destroy
redirect_to blog_path(#blog)
end
def edit
#blog = Blog.find(params[:id])
end
def update
#blog = Blog.find(params[:id])
#blog.update(blog_params)
redirect_to blog_path(#blog)
end
end
def update
#blog = Blog.find(params[:id])
**#blog.update(blog_params)**
redirect_to blog_path(#blog)
end
here you are calling blog_params but you haven't defined it anywhere in your code.
See example here:
See strong params
You need to do this:
#app/controllers/blogs_controller.rb
class BlogsController < ApplicationController
def update
#blog = Blog.find params[:id]
#blog = #blog.update blog_params
end
private
def blog_params
params.require(:blog).permit(:title, :body) #-> in "permit" put the attributes of your "blog" model
end
end
The error is a standard programming issue - undeclared function.
Because you're starting - and to give you more context - the reason this is a problem is because you're calling blog_params when you run the .update method:
#blog.update blog_params
This, as mentioned by Pardeep Dhingra, is the strong params pattern introduced into Rails 4. Strong params prevents mass assignment by explicitly permitting particular attributes of your model.
Whilst your code is okay, you lack the strong params method (in your case blog_params), which Rails needs to complete the request. Adding the method will fix the issue.

Error "param is missing or the value is empty: article"

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

Ruby on Rails error showing when posting

I am a beginner in RoR (8 hours old) and was faced with a problem I can't get past. The tutorial presented in the get started guide on their site goes through setting a post entry example. I get the following error:
NoMethodError in Posts#show
Showing /Users/khalidalghamdi/blog/app/views/posts/show.html.erb where line #3 raised:
undefined method `title' for nil:NilClass
Extracted source (around line #3):
1
2
3
4
5
6
<p>
<strong>Title:</strong>
<%= #post.title %>
</p>
<p>
Rails.root: /Users/khalidalghamdi/blog
Application Trace | Framework Trace | Full Trace
app/views/posts/show.html.erb:3:in `_app_views_posts_show_html_erb__4427112910992919114_2164032300'
Request
Parameters:
{"id"=>"4"}
and this is the tutorial link http://guides.rubyonrails.org/getting_started.html and I am following section 5.6. It is not showing the posted details in the show.html.erb page. What am I doing wrong?
Update: Controller code:
class PostsController < ApplicationController
def new
end
def create
#post = Post.new(post_params)
#post.save
redirect_to #post
end
private
def post_params
params.require(:post).permit(:title, :text)
end
def show
   #post = Post.find(params[:id])
end
end
Set the instance variable #post in PostsController#show action.
Currently, #post variable is set as nil so you are getting undefined method 'title' for nil:NilClass error.
Remove the show action from private. Its not getting called as you have made it private. Hence #post is not set.
For example: (As you haven't shared the code, I am giving an example)
class PostsController < ApplicationController
## ...
def show
#post = Post.find(params[:id])
end
## ...
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Also, a better approach is to add a before_action in your controller where you can set the #post variable in order to avoid redundant code across multiple actions. This makes your code DRY too.
For example:
class PostsController < ApplicationController
## set_post would be called before show, edit, update and destroy action calls only
before_action :set_post, only: [:show, :edit, :update, :destroy]
## ...
def show
## No need to set #post here
end
## ..
private
def set_post
#post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :text)
end
end
Seeing your controller code,you have defined your show action after the private method.
Put it above the private method like this
class PostsController < ApplicationController
def new
end
def create
#post = Post.new(post_params)
#post.save
redirect_to #post
end
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Note:
Any method defined after the private method is also treated as private.

Resources