index.html.haml where line #3 raised:
undefined method `each' for nil:NilClass
Projects #Index
**= #projects.each do |project|**
= project.name
%br
I think because #projects in nil try to add some data after that use that page or you can add condition like if #projects.present? than you can show
This error means you have #projects to be nil
if #projects.present?
# your iterations here
else
# do something else
end
Related
I'm trying to build a follow user feature in my app using Railstutorial.org and I'm hitting a error that doesn't make a lot of sence to me.
In my applicaion.html.haml I render 2 partials,
= render '../assets/javascripts/angular-app/templates/stats'
= render '../assets/javascripts/angular-app/templates/follow_form' if user_signed_in?
The first partial (stats) is this,
- #user ||= current_user
.stats
%a{:href => following_user_path(#user)}
%strong#following.stat
= #user.following.count
following
%a{:href => followers_user_path(#user)}
%strong#followers.stat
= #user.followers.count
followers
And resolves without error,
The second partial is this,
- unless current_user?(#user)
#follow_form
- if current_user.following?(#user)
= render 'unfollow'
- else
= render 'follow'
And throws an error, NoMethodError in Application#angular
Showing /home/alucardu/sites/movieseat/app/assets/javascripts/angular-app/templates/_follow_form.html.haml where line #1 raised:
undefined method `current_user?' for #<# <Class:0x007fbe544651f8>:0x007fbe64cdbb10>
I'm using Devise for my registration and user handeling.
On my application.html.haml I have
.welkom
Welkom
= current_user.name
And this hows my first and last name and in the first partial I also use current_user so why am I getting the error undefined methodcurrent_user?'`
There's no current_user? method in devise, if you want to check whether there's a current user or not you can use user_signed_in? method
i generate to scaffolds name as header and footers.
In application.html.erb I use this code
<%= render template:"headers/index" %>
<%= yield %>
<%= render template:"headers/index" %>
for render action. this is all because of i want to dynamically customize header and footer layout through html_safe method.
I faced error:
undefined method `each' for nil:NilClass
I know both why this error comes when render an action in another controller and how to solve. But still facing this error just because of not able to define an action for application.html.erb in application.rb in my applications_controller.rb
before_filter :set_headers
def set_headers
#headers = Header.all
end
How to solve this problem?
Try this
before_filter :set_headers
def set_headers
#headers ||= Header.all
end
improve you code
I have tried everything, so now I'm asking for help.
NoMethodError in Jobs#index Showing
/Users/sillaspoulsen/Desktop/Coderstravel/app/views/jobs/index.html.erb
where line #15 raised:
undefined method `each' for nil:NilClass
" <% #jobs.each do |job| %> "
Here is my git if that helps: https://github.com/SillasPoulsen/coders
Thank you so much.
In JobsController, change
def index
#job = Job.all
end
To
def index
#jobs = Job.all ## plural jobs
end
You are setting the instance variable as #job in index action and using #jobs (Notice plural) in index.html.erb which obviously would be nil. So, you get the error as undefined method 'each' for nil:NilClass
UPDATE
it gives me the error NoMethodError in Jobs#index- " undefined method
`description' for #" "<%= job.description
%>"
jobs table has a field named discription and you are accessing description which does not exist in jobs table. Hence, the error.
I searched and searched, but nothing solved my problem. Here's my controller:
def show
#topic = Topic.find(params[:id])
#topic.posts = #topic.posts.page(params[:page]).per(2) # 2 for debugging
end
That functions just fine, because the topic view is reduced to two posts. However, when I add this to show.html.erb:
<%= paginate #topic.posts %>
I'm given this error:
undefined method `current_page' for #<ActiveRecord::Relation:0x69041c9b2d58>
Try with:
def show
#topic = Topic.find(params[:id])
#posts = #topic.posts.page(params[:page]).per(2)
end
And then:
<%= paginate #posts %>
If you get pagination errors in Kaminari like
undefined method `total_pages'
or
undefined method `current_page'
it is likely because the AR scope you've passed into paginate has not had the page method called on it.
Make sure you always call page on the scopes you will be passing in to paginate!
This also holds true if you have an Array that you have decorated using Kaminari.paginate_array
Bad:
<% scope = Article.all # You forgot to call page :( %>
<%= paginate(scope) # Undefined methods... %>
Good:
<% scope = Article.all.page(params[:page]) %>
<%= paginate(scope) %>
Or with a non-AR array of your own...
Bad:
<% data = Kaminari.paginate_array(my_array) # You forgot to call page :( %>
<%= paginate(data) # Undefined methods... %>
Again, this is good:
<% data = Kaminari.paginate_array(my_array).page(params[:page]) %>
<%= paginate(data) %>
Some time ago, I had a little problem with kaminari that I solved by using different variable names for each action.
Let's say in the index action you call something like:
def index
#topic = Topic.all.page(params[:page])
end
The index view works fine with <%= paginate #topic %> however if you want to use the same variable name in any other action, it throu an error like that.
def list
# don't use #topic again. choose any other variable name here
#topic_list = Topic.where(...).page(params[:page])
end
This worked for me.
Please, give a shot.
My XML error:
NoMethodError in Admin/xml#index
Showing C:/Rails/asdw/app/views/admin/xml/index.rhtml where line #1 raised:
undefined method `name' for "preview":String
Extracted source (around line #1):
1: <% update_xml("preview") %>
2:
3:
4: <h2>Preview/publish</h2>
My controller:
def index
#photographer = Photographer.find(:first)
#render :layout => false
end
My XML helper:
module XmlHelper
require 'builder'
def update_xml(photographer, output="preview")
xml = Builder::XmlMarkup.new
xml.photographer(:name => photographer.name) do
for group in photographer.groups
xml.group(:name => group.name) do
for project in group.projects
xml.project(:name => project.name) do
for collection in project.collections
xml.collection(:name => collection.name) do
for image in collection.images
xml.image(image.description, :url => image.image, :id => image.id)
end
end
end
end
end
end
end
end
File.open("#{rails_root}/public/xml/#{output}.xml", "w") do |f|
f.puts ("#{xml}")
end
end
end
UPDATE:
Using <% update_xml(photographer, "preview") %>:
NameError in Admin/xml#index
Showing C:/Rails/asdasd/app/views/admin/xml/index.rhtml where line #1 raised:
undefined local variable or method `photographer' for #<#<Class:0x47eb990>:0x47ea238>
NEW UPDATE Using <% update_xml(#photographer, "preview") %>:
oMethodError in Admin/xml#index
Showing C:/Rails/asdfsadf/app/views/admin/xml/index.rhtml where line #1 raised:
undefined method `groups' for #<Photographer:0x45ca2d8>
Extracted source (around line #1):
1: <% update_xml(#photographer, "preview") %>
2:
3: <h2>Preview/publish</h2>
Could you please try using "<% update_xml(photographer, "preview") %>" instead of <% update_xml("preview") %> in Admin/xml#index, let me know if you get error again!
Make sure in your Photographer model you have a groups association such as:
has_many :groups