Ask Again Later Undefined Method for Nilnilclass
Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to become back up. Collaborate here on code errors or bugs that yous need feedback on, or request for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Simply Treehouse students can annotate or ask questions, simply non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven mean solar day gratuitous trial for new students. Get access to thousands of hours of content and a supportive community. Showtime your gratuitous trial today.
undefined method `name' for nil:NilClass
Rendered employees/show.html.erb within layouts/application (110.6ms) Completed 500 Internal Server Fault in 116ms (ActiveRecord: 7.4ms)
ActionView::Template::Mistake (undefined method `name' for nil:NilClass): xvi: < div course = "entry" > 17: < stiff > <%= entry.country %></ strong > 18: <%= entry.time %> 19: <%= entry.account.name %> 20: <%= entry.created_at %> 21: </ div > 22: <% end %>
84 Answers
I've no idea if this is to do with the issue you accept but you lot take double-defined a variable in your accounts_controller
- I'g just cruising through your code looking for stuff that looks off; this is the commencement I plant:
class AccountsController < ApplicationController def index @accounts = Business relationship . all # delete this line @accounts = Client . all end def show @account = Account . discover ( params [ :id ] ) end end
Indicate @accounts
at the Customer
; delete the Account
line.
Let me know if that helps,
Steve.
its non working Steve, i did the aforementioned as u said.
I take four employees, when i click on beginning two employee proper name it gave me the error-
NoMethodError in Employees#show Showing /Users/jitendragoyal/biller/app/views/employees/show.html.erb where line #19 raised:
undefined method `proper name' for nothing:NilClass Extracted source (effectually line #19):
17 < strong >< %= entry.land %></strong> 18 <%= entry . time %> 19 <%= entry.business relationship.proper noun %> 20 <%= entry . created_at %> 21 </div> 22 < % terminate %>
just when I click on the other two employees name it'south working. i did everything, i take restarted the server many times.
Y'all have probably created the first two employees before adding functionality to the database. Delete the two causing a trouble inside the irb console. Use rails c
and go find those two employees and delete them, then supersede them with new one that reflect the new structure you have created.
Steve.
i typed - TimeEntry.all.each { |t| t.update_attributes(country: "submitted") }
ok.i deleted all the employees through the console and create a new employee.but problem is same
No problem Steve, you lot take to sleep.I'll endeavor to solve the problem. thank you and then much.
Still I have same problem. don't know why?
This is my business relationship.rb file code-
class Account < ActiveRecord::Base of operations
has_many :account_entries, dependent: :destroy validates :name, presence: truthful, length: {in: 1..70, message: "Delight utilise a decent name, sir."}, uniqueness: truthful validate :your_name_is_not_dumb def your_name_is_not_dumb if name.include?("dumb") errors.add(:proper name, "is impaired") end cease def update_balance! update_attributes( balance: cocky.account_entries.sum(:corporeality) ) stop
end
I typed - TimeEntry.all.each { |t| t.update_attributes(state: "submitted") }
now the new error in runway console -
TimeEntry Load (0.9ms) SELECT account_entries.* FROM account_entries WHERE account_entries.blazon IN ('TimeEntry') (0.3ms) Begin
Account Load (0.6ms) SELECT accounts.* FROM accounts WHERE accounts.id = 1 LIMIT 1
SQL (2.1ms) UPDATE account_entries SET country = 'submitted', updated_at = '2016-03-08 23:46:15' WHERE account_entries.id = ane (ii.6ms) ROLLBACK
NoMethodError: undefined method `update_balance!' for naught:NilClass
Account.count
(37.7ms) SELECT COUNT(*) FROM accounts
=> 1576
Employee.first
Employee Load (168.0ms) SELECT accounts
.* FROM accounts
WHERE accounts
.blazon
IN ('Employee') ORDER By accounts
.id
ASC LIMIT 1
=> #Employee id: 1582, type: "Employee", name: "jyoti", email: nix, well-nigh: nil, created_at: "2016-03-09 00:10:35", updated_at: "2016-03-09 00:ten:35", city: nil, zipcode: zilch, land: nil, employees: nil, balance: nil
Employee.count
(3.4ms) SELECT COUNT(*) FROM accounts
WHERE accounts
.blazon
IN ('Employee')
=> 2
means output of this query is correct.I have merely 2 employees.
My problem is when I typed - TimeEntry.all.each { |t| t.update_attributes(state: "submitted") }
now the new fault in track console -
TimeEntry Load (0.9ms) SELECT account_entries.* FROM account_entries WHERE account_entries.blazon IN ('TimeEntry') (0.3ms) Brainstorm
Account Load (0.6ms) SELECT accounts.* FROM accounts WHERE accounts.id = 1 LIMIT 1
SQL (2.1ms) UPDATE account_entries Prepare state = 'submitted', updated_at = '2016-03-08 23:46:15' WHERE account_entries.id = i (2.6ms) ROLLBACK
NoMethodError: undefined method `update_balance!' for nil:NilClass
TimeEntry.all.each { |t| t.update_attributes(state: "submitted") }
This query is not working.
If u demand some file code of this project Sick send it to u.
All the query is working correctly in console except- TimeEntry.all.each { |t| t.update_attributes(country: "submitted") }
Means problem is all the same aforementioned.I can't effigy out the problem...
OK, then the problem is when operating on the account_entries.id. It sees the method update_balance
every bit undefined. That'south not the example, as nosotros know, it is in your code. The issue is that the method is undefined for an object of nil::NilClass
. And so the outcome is, again, the business relationship entry, or business relationship, pulling dorsum a nil.
Walking through the code, the update_balance!
method is held in the account
model but it is called from the account_entry
model via its own customer method, update_account_balance
. Inside that method, information technology calls business relationship.update_balance!
. That'southward where information technology fails but that doesn't mean the lack of account
stems from at that place. That could happen anywhere, annoyingly. I wonder if the lack of saving of the account
instance at that point would crusade information technology to drop the case?
Worth a effort - add this line in the update_account_balance!
method inside AccountEntry
- here's the whole method including the new line:
def update_account_balance! account . update_balance! business relationship . salvage end
Information technology's a long shot, only worth a try.
AccountEntry.count
(4.2ms) SELECT COUNT(*) FROM account_entries
=> 21
AccountEntry.find(1)
AccountEntry Load (0.4ms) SELECT account_entries
.* FROM account_entries
WHERE account_entries
.id
= one LIMIT i
=> #TimeEntry id: 1, time: 1.0, customer_id: nil, employee_id: 1578, created_at: "2016-03-04 05:37:07", updated_at: "2016-03-04 05:59:26", account_id: 1, type: "TimeEntry", amount: twoscore, state: cypher i
How to set up the state of account_entries.id = 1 to 'submitted'.
Two things here; kickoff your question, second some other thoughts ...
To set the time entry, something like:
a = AccountEntry . detect ( 1 ) a . land = 'submitted'
If that throws an error it should exist clear what'due south incorrect with the code. (Information technology works!)
Should the Time Entries you load via the web page testify up? They're not doing in my version of your app.
Load you server and go to /employees visit i of their show
pages similar:
Load the panel and blazon TimeEntry.count
, and then add a fourth dimension entry on the folio so ther should exist another TimeEntry
... blazon reload!
into console so TimeEntry.count
once more. I get the same value returned - null in my case. Do you lot become an incremented number?
If the land is already 'submitted', it'll mistake out as the object will return imitation
, a member of nil::NilClass.
And so, if that is the case, the use of update_attributes
is causing the land setting to neglect; returning simulated, not an object of account. Any subsequent phone call of an account method on faux will throw the mistake you lot're seeing. I think.
When I typed -TimeEntry.all.each { |t| t.update_attributes(state: "initial") }
instead of "submitted", i inverse the state to "initial". Then Im not getting fault.
Tried a bit more in console.
First, I created an Business relationship and an AccountEntry:
a = Account . create ( name : "AnAccount" ) b = AccountEntry . create ( account_id : a . id ) b . update_account_balance!
That works ... previously, that was failing due to the lack of association with the Business relationship id. I'm trying to figure out why that was happening.
Your Commands is working.
Problem is that, When nosotros prepare the state = "submitted", query is not executing.
Sorry Steve for today to bother u.
TimeEntry.all.each { |t| t.update_attributes(state: "initial") }
SyntaxError: (irb):9: syntax fault, unexpected & ...t| t.update_attributes(land: "initial") } ... ^ (irb):nine: syntax mistake, unexpected ')', expecting '}' ...tes(state: "initial") }
TimeEntry.all.each { |t| t.update_attributes(state: "submitted") }
This Command is not working
sorry for late respond, I lost the cyberspace connection.
TimeEntry.all.each { |t| t.update_attributes(state: "submitted") }
TimeEntry Load (0.6ms) SELECT account_entries
.* FROM account_entries
WHERE account_entries
.type
IN ('TimeEntry') (0.2ms) Begin
Account Load (0.3ms) SELECT accounts
.* FROM accounts
WHERE accounts
.id
= 1 LIMIT i (0.3ms) ROLLBACK
NoMethodError: undefined method `update_balance!' for nil:NilClass
from /Users/jitendragoyal/biller/app/models/account_entry.rb:24:in `update_account_balance!'
accept ur time,don't worry.
This is a pain, and there must exist a way to practise this in console but I can't figure it out.
So, I've created a new view folder called time_entries
and inside there I've created a view called index.html.erb
. That file holds this lawmaking:
< % @time_entries.each practice | te | %> <%= te.account_id %> < % finish %>
So that loops through all the fourth dimension entries and outputs the account_id. BUT, @time_entries
doesn't exist. And nor does the index
action. And so, go to your time_entries_controller.rb
and add:
def index @time_entries = TimeEntry . all stop
Now, we demand a route else that'll not work at all. So find your config/routes.rb
file and add this:
get 'time_entries' , to : 'time_entries#index'
Yous could probably just add together resource :time_entries
to the root of the file, rather than inside a do
cake - same difference.
So, what nosotros've done there is created a loop in the view to iterate over all the fourth dimension entries. In the controller, we've created an action for that type of view, the alphabetize
activeness, and take created the @time_entries
variable that'southward bachelor across all the MVC files. To become the page to work, we've added a route that sends all requests for /time_entries
to the right view, using the correct controller.
I promise that made sense ... in my browser, I have 4 numbers, one for each time entry, and no null values. Accept a become and run into what y'all get, and I'll refine this a little further, to see if I can become it to right the issues you're having.
Hi Steve,sorry yesterday my internet was doing problem. Stiil i thou getting same error.
yeah i tried, but still not working.
I retrieve nosotros have different timings.means here is morning at present and there?
Means same problem as terminal nighttime.
Land = Submitted is not working
Yes. My code wasn't at that place to fix the error; that exists within your database. It is there to identify what'south wrong with your database.
Take you implemented all the lawmaking changes in my post nigh time_entries
?
Yes I have implemented.But Nothing brand difference.....what should i do?
Hi Steve, I tried to solve the result.I retrieve, the problem is betwixt account_id and time_entries.I m sharing some screenshots to understand the issue--
This is the Customer list.
I have some time_entries for few customers.like i have time_entry for customer "Kessler Denesik and Mayer and Sons".When I clicked on the Client link for "Kessler Denesik and Mayer and Sons" on http://localhost:3000/accounts page it give me error--
It didn't give me fault for those customers for which i don't have time entries.
I don't retrieve the command. I did follow the video.give me a infinitesimal.
I have already run your code merely it doesn't work.
You are talking about these ii lines -
become 'time_entries', to: 'time_entries#index'
ok, i did it.it give me blank folio--
I reverted the lawmaking and result is-
Yes you are right Steve.This is the longest thread in treehouse customs.now what i should to do adjacent..
Cheers Steve for your help.but There is no project file regarding to biller.Hampton didn't explicate many things, and so I didn't learn too much.Thats why Im not expert at ACTIVE Record Basics.
For some Download it says u demand to upgrade to pro.i don't have pro subscription.
how r u? I want to enquire one question.
how to create a rail case for existing database?
i do have access of company database through VPN simply i dont know how to access the carmine code of that database.I can connect the my laptop to that server.
i have access of company database server , which i am accessing via mysql workbench. now he gave me chore , to perform task i need to access ruby lawmaking and i am so new , i don't know if i tin access rails using server database ?
is it possible to access rails instance when server access been given. ?
i am using tunnelblick to connect company DB.
Try running a panel on the Rail instance. Navigate to the root of the server and run rails c
- that should give you access to the instances and tables. Without knowing what you're dealing with, I'chiliad but going to be guessing hither!
Source: https://teamtreehouse.com/community/undefined-method-name-for-nilnilclass
Belum ada Komentar untuk "Ask Again Later Undefined Method for Nilnilclass"
Posting Komentar