NoMethodError in rails - ruby-on-rails

The extracted source is below :
Showing /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/templates/rescues/diagnostics.erb where line # raised:
private method `gsub' called for #<NoMethodError: undefined method `closed?' for nil:NilClass>
Extracted source (around line #):
RAILS_ROOT: /home/sharath/Desktop/RORWorkspace/ITPLOW1
It was working before installing Sunspot: A Solr-Powered Search Engine for Ruby.
I am working in Ubuntu 10.04.

I'd need to see the full stacktrace to be sure, but this is actually probably an unhelpful HTTP connection error message bubbling up out of RSolr (the library the Sunspot uses for the low-level Solr connection). Is Solr running (i.e., did you run rake sunspot:solr:start)? Can you access http://localhost:8982/solr/admin ?

What's probably happening is you're attempting to do a substitution on some variable which you thought you were initializing, but neglected to give a real value.
For instance, if you had a form where for a Message and one of the the properties you want is the content, you would normally retrieve that information in the controller with
params[:message][:content]
And if you wanted to filter it, you would do something like
params[:message][:content].gsub(/<[^>]*>/,"")
But if the user didn't enter anything into the content field, the params[:message][:content] variable wouldn't be set. Therefore it's null and you're attempting to do nil.gsub

Related

Rails jbuilder error: ActionView::Template::Error (undefined method

I have a Rails 6 app and we're using jbuilder to define the shape of JSON responses.
Recently, I started getting the following error:
ActionView::Template::Error (undefined method `empty?' for #<Account:0x0000000116743030>):
The stack trace points to this block inside a jbuilder file:
json.account do
json.call(#account, *Account::APP_FIELDS)
json.logo_url #account.logo
end
If I comment out both of the lines inside the block, the error goes away. If I remove either of the lines and leave the other, the error returns. The stack trace just points me to the first line of the block.
What's going on? How do I fix this?
Figured it out!
Higher in the jbuilder file, a partial is included that adds the same account key. This was causing some kind of name collision.
I stumbled on this by temporarily testing with my key renamed accountt and suddenly it worked. That's when I realized it was conflicting name that was causing the issue. I didn't see it because it was in a different file and the error message was confusing. If your jbuilder file is complicated and you think you're running into this same issue, this is an easy way to test.

Ruby on Rails: NoMethodError undefined method `get_yaml' for String

I tried to deploy a non public project but sometimes when tried to use
report pages (based on prawn) I have this problem:
NoMethodError (undefined method `get_yaml' for #<String:0x7fdecffd3738>):
The piece of code is:
report_content = report.draw(report_content.get_yaml)
Someone know: where are get_yaml come from?
Thanks.
The method #get_yaml is not a part of of the standard Ruby library, although #to_yaml is defined if the yaml library has been loaded. #get_yaml, if it exists, is defined by the application or by some library that the application is loading.

Rails Console 'helper' and 'app' Appear Incomplete

Using ruby 2.3.0 and rails 4.2.5.1, I'm trying to figure some things
out using the rails console. I have several instances where app or helper seem like they have a method I want, but I often end up with the NoMethodError message. This happens even in a brand-new application where I've edited nothing and given no special options to the rails new command. For instance:
irb(main):015:0* helper.timez
... here I hit TAB to get:
irb(main):015:0* helper.timezone
... and hit ENTER, only to find:
NoMethodError: undefined method `timezone' for #<ActionView::Base:0xc662b80>
Why would tab-expansion in the console expand to methods that don't
exist? More importantly, if they're supposed to be there, why aren't
they? What can I be doing wrong?

Log full stacktrace in rails including all gems

Currently when I look in the log files of my rails application I get stacktraces like:
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/concerns/example.rb:192:in `rescue in create_example'
app/controllers/concerns/example.rb:163:in `create_example'
app/controllers/concerns/example.rb:11:in `example'
app/controllers/example_controller.rb:39:in `create'
The error is triggered from a second project which is included as gem.
On line 192 in the example.rb (concern) we use some classes from that included gem and in that class the real exception occurs.
Another example:
ZeroDivisionError (divided by 0):
app/controllers/dummy_controller.rb:15:in `index'
And on line 15
test_object.divide_by_zero
test_object is an instance of a class defined in the included gem
I want rails to display and log the full stacktrace including all or specific gems but I can't figure out how to do this. Does someone know how to do this? or someone that can give me a push in the right direction?
Thanks!!!
It's used like this:
Rails.backtrace_cleaner.remove_silencers!
Depending on what version of rails you are using this may help:
http://api.rubyonrails.org/classes/ActiveSupport/BacktraceCleaner.html#method-i-remove_silencers-21

.to_date method failing

I am using rails 3.2.16 and ruby 1.9.3
I get this error from the localhost browser after rails s.
undefined method `to_date' for nil:NilClass
like this
NoMethodError in Products#new
Showing /Users/main/railscasts-episodes/episode-350/store-
after/app/views/products/_form.html.erb where line #27 raised:
undefined method `to_date' for nil:NilClass
in irb> mode it works only when I
require 'date'
Now my question is about my rails project. In which file in my project I should add
require 'date'
I added it into my model it did not work. Or, maybe I need to install any specific gem? to make this work
The problem you have isn't an issue with to_date, it's a problem with the object you're trying to perform the method on
Your issue will either be:
You have not declared the variable / object
Your variable / object is not populated with any data
I'd highly recommend posting the controller action you're using to invoke the to_date method. Obviously, there may be an issue with the way you're calling it, but the immediate issue is you're trying to deal with an unpopulated variable

Resources