Please help: undefined method `each' for nil:NilClass - ruby-on-rails

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.

Related

index.html.haml Ruby on Rails

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

On Ruby-on-Rails it's possible to pass variables from controller to view through respond_to?

I want to submit a message and after that to show a message and open a popup window. The message is saved properly on DataBase, but the message don't show up , instead I receive this
NoMethodError in Private::Conversations#create
Showing /var/www/appror/app/views/private/conversations/_open.js.erb where line #3 raised:
undefined method `id' for nil:NilClass`
The application run on
Rails version 5.2.2;
Ruby version 2.5.3-p105 (x86_64-linux);
RubyGems version 2.7.6;
Rack version 2.0.6
after the message is save, in controller
/app/channels/private/conversation_channel.rb
class Private::ConversationsController < ApplicationController
def create
recipient_id = Post.find(params[:post_id]).user.id
conversation = Private::Conversation.new(sender_id: current_user.id, recipient_id: recipient_id)
if conversation.save
respond_to do |format|
format.js {render partial: 'posts/show/contact_user/message_form/success'}
end
....
end
from
/app/views/posts/show/contact_user/message_form/_success.js.erb
where it's
<%= render 'private/conversations/open' %>
it should show the message
/app/views/private/conversations/_open.js.erb
where
var conversation = $('body').find("[data-pconversation-id='" +
"<%= #conversation.id %>" +
"']");
Normally it should go forward and show the message and popup, but instead I'm receiving
NoMethodError in Private::Conversations#create
Showing /var/www/appror/app/views/private/conversations/_open.js.erb where line #3 raised:
undefined method `id' for nil:NilClass`
After debugging I saw that <%= #conversation %> is empty... so, it makes sense the error. But how I can pass #conversatio from controller to view ? It's possible to pass this using
respond_to do |format| format.js
?
Thank you!
EDIT:
thank you #max, indeed #conversation = .... solved the issue
You're missing an # in your conversation variable in your controller, and your variable is falling out of scope.
#conversation = Private::Conversation.new(sender_id: current_user.id, recipient_id: recipient_id

undefined method `each' for nil:NilClass in rails while using render a action of different controller in application controller

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

trouble with the logger

i had an error when trying some logs:
NoMethodError in RelationController#create
undefined method `attributes' for #<ActiveRecord::Relation:0x431e650>
this is my controller
def create
follow = User.find(params[:relation][:following_id])
current_user.following << follow
logger.debug "follow: #{follow.attributes.inspect}"
logger.debug "current_user: #{current_user.attributes.values}"
logger.debug "following: #{current_user.following.attributes.values}"
redirect_to follow
end
what can i do to fix that, please help me!!
The cause of your error is this line
logger.debug "following: #{current_user.following.attributes.values}"
You're calling attributes on current_user.following which is an ActiveRecord::Relation object. If you want to log, you have to loop through each of current_user.following and call attributes on it
current_user.following.each do |follow|
logger.debug "following: #{follow.attributes.values}"
end

Rails 3 undefined method `model_name' for NilClass:Class

I have an accounts resource. When going to /accounts/new, I get:
undefined method `model_name' for NilClass:Class
The error is on line 1 in new.html.haml. Here's my code:
My view: new.html.haml
1. = form_for #account do |f|
2. - if #account.errors.any?
#error_explanation
3. ......
My controller new method:
def new
#account = Account.new
end
What am I doing wrong here?

Resources