undefined method `each' for nil:NilClass schneems reddit example - ruby-on-rails

I thought that adding #links=Link.all to my controller would fix this problem.
I did make sure that I have valid entries in my Link DB by looking at Link.count=6
I am getting the following error:
NoMethodError in Pages#index
Showing /Projects/reddit_on_rails/app/views/pages/index.html.erb where line #6 raised:
undefined method `each' for nil:NilClass
<ul>
<% #links.each do |link| %>
<li>link.url</li>
<li>link.title</li>
<% end %>
My controller:
class LinksController < ApplicationController
#Added #links =Link.all did not solve the problem
def index
#links = Link.all
end
def show
end
def new
#link = Link.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #link }
end
end
def create
#link = Link.new(link_params)
if #link.save
#render "new"
redirect_to "/"
else
# This line overrides the default rendering behavior, which
# would have been to render the "create" view.
render "new"
end
end
private
def link_params
params.require(:link).permit(:title,:url)
end
end

According to your description, you got following error message
NoMethodError in Pages#index
Showing /Projects/reddit_on_rails/app/views/pages/index.html.erb where line #6 raised:
According to error message you have problem in Pages controller inside index method. So you need to fix the problem inside Pages controller not inside Links controller.
Do you have method called index inside your Pages controller?

#links is nil because you are not setting it anywhere in your controller with something like #links = Link.all.
You cannot call each on nil.

What view are you in? Assuming index.html.erb.
in your controller, try adding
def index
#link = Link.all
end
If that doesn't work, verify you have any links at all. In terminal instead of typing rails server type
rails console
Link.all
If that comes back as [] then you don't have any in your database.

Related

undefined method `model_name' for nil:NilClass (NoMethodError)

The error: undefined method `model_name' for nil:NilClass (NoMethodError)
I'm receiving this error when trying to render the haml below:
%section#banner
.row
.medium-12.columns
%h2 Add Testimonial
= simple_form_for(#testimonial) do |f|
.row
.large-6.columns
= f.input :text, as: :text,
placeholder: 'Use this space to write a testimonial about the event(s) you participated.'
.row
.large-6.columns
%p.description
= sanitize('Any testimonial along with your name and profile picture might be used for the promotion of codebar (website, prospectus, etc).')
.row
.large-12.columns.text-right
= f.submit 'Submit testimonial', class: 'button'
The controller is the following:
class TestimonialsController < ApplicationController
before_action :authenticate_member!
def get_testimonial
testimonial = Testimonial.where(member_id: testimonial_member_id)
invitations = current_user.workshop_invitations.accepted_or_attended
if invitations.any? and testimonial.blank?
render 'new'
else
render 'show'
end
end
def show
#testimonial = Testimonial.find(testimonial_member_id)
end
def new
#testimonial = Testimonial.new
end
def create
#testimonial = Testimonial.new(testimonial_params)
#testimonial.member_id = current_user
#testimonial.public = false
if #testimonial.save
redirect_to #testimonial
else
render 'new'
end
end
private
def testimonial_params
params.require(:testimonial).permit(:text)
end
def testimonial_member_id
params[current_user]
end
end
May someone help me see why is returning nil? If the variable is the same I'm passing on the new function?
AFAIK simple_form_for(#testimonial) will try to call #testimonial.model_name so that's where the problem most likely originates.
If you go through the get_testimonial controller, you can end up at:
render 'new'
and that will render the HAML in question. But, notice that nothing in get_testimonial initializes #testimonial so get_testimonial will end up trying to simple_form_for(nil).
Changing the bottom of get_testimonial to something more like this:
if invitations.any? && testimonial.blank?
#testimonial = Testimonial.new
render 'new'
else
render 'show'
end
Your show template presumably needs a #testimonial as well so you might want to say #testimonial = testimonial.first before render 'show' too.
Also, I've changed your and operator to && since you're generally better off pretending that and doesn't exist. The low precedence of and and or cause a lot of problems so you're better off sticking to && and ||.
I'm not sure of the logic for testimonials so you might be able to go with something more like:
def get_testimonial
#testimonial = Testimonial.find_by(member_id: testimonial_member_id)
invitations = current_user.workshop_invitations.accepted_or_attended
if invitations.any? && !#testimonial
#testimonial = Testimonial.new
render 'new'
else
render 'show'
end
end
You might want to revisit your testimonial_member_id method as well, this:
def testimonial_member_id
params[current_user]
end
looks odd, maybe it should be params[:id] instead.

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])

error of undefined method `id' for nil:NilClass

I'm new to rails and I have this no method error
show.html.erb
<h1>Profile Page for <%= #item.id %></h1>
<p><%= #item.content %></p>
items_controller.rb
class ItemsController < ApplicationController
def new
#item = Item.new
end
def create
item = Item.new(item_Params)
if item.save
redirect_to user_path(item.user)
else
render 'new'
end
end
private
def item_Params
params.require(:item).permit(:user_id, :content)
end
end
I want to create a show page for items/:id and I want to be able to view each item by their id, how should I create it?
this is my show page
Show page for which resource? Is it /users/:id (since you're redirecting to it) or items/:id? Anyway, you need to define #item in show action of corresponding controller. If you add more information about corresponding controller and user-item association, I can help you with it
UPDATE:
Just add to the ItemsController
def show
#item = Item.find(params[:id])
end
Make sure that you have a line resources :items inside routes.rb.
I think you need to read more about rails controllers in guides
BTW: it is a convention in ruby to use snake_case for method naming. Should be 'item_params', not 'item_Params'
You need to add show action which is for show page and You find the item corresponding its Id. so you just write below action in your controller.
def show
#item = Item.find(params[:id])
end
You can find your item in your Item model using find method.

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.

Rails: Pasting code from one html.erb file to another creates error: undefined method `url' for nil:NilClass

I have the code:
<h1><%= #app.name %></h1>
in show.html.erb and it runs fine. However when I paste that code to newsfeed.html.erb, it gives the following error message:
NoMethodError in Reviews#newsfeed
undefined method `url' for nil:NilClass
From reading other similar questions I think I need to make a change to reviews_controller.rb. This is (I think) the relevant code in that file:
def newsfeed
#reviews = Review.all
end
And the relevant code in the main AppsController
require 'Review'
class AppsController < ApplicationController
...
def show
#app = App.find_by_name(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #app }
end
end
I believe the solution is to add some code below def newsfeed but I'm not sure what to add. Or maybe that's not even the solution.
Thanks for any help!
#app is nil. In the show action you set it like so
#app = App.find_by_name(params[:id])
But you forgot to add it to the newsfeed action!

Resources