Why does undefined method `-#' for nil:NilClass mean (haml)? - ruby-on-rails

I have this rails view partial and I just can't see the issue:
%span{class: "show_hide #{show_hide}"}
---------------------
= first_field_focus
---------------------
- if first_field_focus == "1"
- puts '++++++++++++'
- puts 'y'
- puts '.............'
- puts '.............'
- puts '==='
%a{href: '#', :data => {toggle_description_length: 'toggle'}} # Line 11
= raw(txt)
but I just keep getting:
NoMethodError in Links#index
Showing /home/durrantm/Dropnot/webs/rails_apps/linker/app/views/links/_toggle_details_link_bold.html.haml where line #11 raised:
undefined method `-#' for nil:NilClass
Extracted source (around line #11):
...

It means you're trying to call the - operator on something that is nil. Probably those --------------------- lines are being interpreted as a code line that's a chain of minus signs and it's confusing things. Try making it \--------------------- instead.

Related

Undefined local variable but the variable is defined

In a Rails project, I have following error:
undefined local variable or method ` response' for #<Deliveries::CheckJobService:0x00007fce8548dd60> Did you mean? response
And here is the code:
delivery_status = response['status']
I don't see the error
I try new things, and the error is even weirder:
def call
return false if #order.stuart_job_id.nil?
response = stuart_check_job
if response.nil?
#order.update(delivery_status: 'sth went wrong')
else
delivery_status = 2
delivered_at = 4
#order.update(delivery_status: delivery_status, delivered_at: delivered_at)
end
return true
end
This is the error:
undefined local variable or method ` 2' for #<Deliveries::CheckJobService:0x00007fce8490fc40>
I don't see any single quotation

Rails 5: ActiveRecord collection index_by

Upgraded app to Rails 5 using the index method. The issue is that it is not incrementing to the next ActiveRecord collection record. The below code below use to work in Rails 4.0. Tried with index_by.
def next_question
index = campaign.quiz_questions.index self
campaign.quiz_questions[index + 1]
end
Debugger
(byebug) campaign.quiz_questions.index
*** NoMethodError Exception: undefined method `index' for #<QuizQuestion::ActiveRecord_Associations_CollectionProxy:0x007f80012d71b0>
Did you mean? index_by
Using index_by
(byebug) index = campaign.quiz_questions.index_by
#<Enumerator: #<ActiveRecord::Associations::CollectionProxy [#<QuizQuestion id: 113, campaign_id: 492, message: "Where did Hullabalooza's freak show manager send H...", created_at: "2016-07-20 20:50:32", updated_at: "2016-07-20 20:50:32">]>:index_by>
Index + 1
(byebug) index + 1
*** NoMethodError Exception: undefined method `+' for #<Enumerator:0x007fc4db445960>
nil
changed it to find_index method. Now it's working
def next_question
index = campaign.quiz_questions.find_index self
campaign.quiz_questions[index + 1]
end

Ruby error: undefined method `casecmp' for nil:NilClass

I have the following code (with a few debug lines added):
Ruby:
re_dict = {}
re_dict['state'] = 'pending' #set initial status to pending
puts re_dict, re_dict.class.to_s
puts re_dict['state'], re_dict['state'].class.to_s
puts re_dict['state'].casecmp('pending')
while re_dict['state'].casecmp('pending') == 0 do
stuff
end
Output
state: pending
state class: String
class compared to 'pending': 0
Completed 500 Internal Server Error in 66ms
NoMethodError (undefined method `casecmp' for nil:NilClass):
What is causing this? How am I losing the value of my hash?
This will happen only when you remove 'state' key from re_dict hash inside your while loop:
while re_dict['state'].casecmp('pending') == 0 do
puts re_dict
re_dict = {}
end
#=> {"state"=>"pending"}
#=> NoMethodError: undefined method `casecmp' for nil:NilClass
Since, key 'state' is not available anymore, calling re_dict['state'] will give nil, that's why you're getting undefined method casecmp' for nil:NilClass

Rails syntax error missing parenthasizes

I am getting a weird syntax error from rails that I don't understand.
GETTING THE FOLLOWING ERROR:
Showing /home/action/workspace/clinio/app/views/tasks/_task.html.erb where line #3 raised:
/home/action/workspace/clinio/app/views/tasks/_task.html.erb:3: syntax error, unexpected ';', expecting ':'
';#output_buffer.append=( image...
^
Extracted source (around line #3):
<% #uncompletedtasks = #task if #uncompletedtasks?%>
<li id="task_">
<div><%= image_tag "26-mini-gray-checkmark.png" %>
<%= #uncompletedtasks.task %>
</div>
</li>
Trace of template inclusion: app/views/tasks/_task.html.erb, app/views/layouts/application.html.erb
Rails.root: /home/action/workspace/clinio
Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:35:in _app_views_layouts_application_html_erb__122972711486791642_46610700'
app/controllers/users_controller.rb:16:inindex'
You don't need that question mark at the end.
<% #uncompletedtasks = #task if #uncompletedtasks %>
(the purpose of this code still eludes me, though. Why would you want overwrite #uncompletedtasks only if it has value?)

rescue Nokogiri error

I've a simple script that looks at Twitter username and gets me the location. But some of the username doesn't exist and I get error:
/usr/lib/ruby/1.8/open-uri.rb:277:in `open_http': 404 Not Found (OpenURI::HTTPError)
I've tried to rescue it, but I can't to make it work. Can anyone help? Thanks
a = []
my_file = File.new("location.txt", 'a+')
File.open('address.txt', 'r') do |f|
while line = f.gets
url = "http://twitter.com/#{line}"
doc = Nokogiri::HTML(open(url, 'User-Agent' => 'ruby'))
doc.css("#side #profile").each do |loc|
my_file.puts "http://twitter.com/#{line} #{loc.at_css(".adr").text}"
puts line
end
end
end
I also need help rescuing another error:
twitter.rb:14: undefined method `text' for nil:NilClass (NoMethodError)
Thanks.
Double quotes inside the other double quotes! Use single quotes for the call to at_css():
my_file.puts "http://twitter.com/#{line} #{loc.at_css('.adr').text}"
Turns out a simple rescue StandardError did the trick.

Resources