Heroku Array Unshift Method Rails - ruby-on-rails

I have a working rails app on my local machine. I updated my Heroku and started to test it. One of my views requires the controller to add elements to an array using unshift.
When I go to this view on the web, I get an error We're sorry, but something went wrong.
I went into $heroku logs and the most recent errors are:
2014-02-28T02:08:26.650021+00:00 app[web.1]: NoMethodError (undefined method `unshift' for #<ActiveRecord::AttributeMethods::Serialization::Attribute:0x007fe57862f588>):
2014-02-28T02:08:26.650021+00:00 app[web.1]: app/controllers/users_controller.rb:32:in `show'
Any ideas how to fix this?
my controller function looks like this:
#user.daily = #user.daily.unshift(day)
#user.daily is a serialized array

#Mhsmith21, if day is an object and if you are rails 4+ then my suggestion is to use build instead of unshift.
If you are using unshift to add the object on first position then use build and reverse the array.
As unshift does not work for ActiveRecord::Associations::CollectionProxy.

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.

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?

Need help diagnosing a 500 error in Heroku

I built a webapp for a class that uses a weather API to display the local weather on a single page. I've uploaded the app to Heroku, but when I try to view the page, I get a 500 error. I've looked at the logs, but I can't decipher what is going wrong exactly. Can someone please take a look at the logs and point out what's going wrong?
Here is a link to the logs: https://gist.github.com/allredbm/70daea5a4c372c644cac
The error is as follows:
app/controllers/home_controller.rb:18:in `index'
NoMethodError (undefined method `[]' for nil:NilClass)
On line 18 of your home_controller (index action), you're referencing a variable with a []. The variable isn't defined.
You need to make sure the variable is defined before trying to reference it.
You can view the whole backtrace by doing either:
Set config.consider_all_requests_local = true in config/environments/production.rb - this is the quickest
Add New Relic (free), configure it, reproduce the 500, wait 1 minute and go into the errors tab in New Relic - this is the preferred as you don't expose the error to the public

Herkou class changes not detected

I'm using Herkou for some Rails 3 hosting. I have pushed code to Heroku, and I am getting the following error from the logs:
NoMethodError (undefined method `format_date' for #<Project:0x00000001938f40>):
This method does exist! I can for example so this:
$heroku console
>> Project.format_date('2011-07-07 10:50')
=> "2011-07-07"
So my code appears to be deployed. Project responds to format_date.
I also tried heroku restart with no success.
Can anyone help me? Any and all suggestions are appreciated!
Are you sure you are calling format_date on the class itself, like you do in your example, and not on an instance of the class? I.e. Project.new.format_date("2011-07-07 10:50")

FriendlyId: undefined method `use_slug?' for nil:NilClass

I am creating a new environment besides production. I copied all the configurations from my production environment, changing what needed to be changed.
As it is the servers do start, but when I do a query I get this exception:
NoMethodError (undefined method `use_slug?' for nil:NilClass):
vendor/gems/friendly_id-2.3.4/lib/friendly_id/active_record2/finders.rb:65:in `slugged?'
vendor/gems/friendly_id-2.3.4/lib/friendly_id/active_record2/finders.rb:43:in `finder_class'
vendor/gems/friendly_id-2.3.4/lib/friendly_id/active_record2/finders.rb:37:in `finder'
vendor/gems/friendly_id-2.3.4/lib/friendly_id/active_record2/finders.rb:32:in `method_missing'
vendor/gems/friendly_id-2.3.4/lib/friendly_id/active_record2/slugged_model.rb:149:in `find_one'
app/controllers/home_controller.rb:5:in `index'
The line in question does this:
#page = Page.find("home")
I am using FriendlyId 2.3.4, and Rails 2.3.4. The code is the same for the production environment, and it's working just fine there, so I'm not really sure on what's going on here...I could see that the line where the exception gets raised does
friendly_id_config.use_slug?
so for a reason I'm not aware of friendly_id_config is nil.
Thanks for any guidance on this problem
I found a workaround to this issue by loading the server using the production environment.
I was trying to use a 'preprod environment, and somehow FriendlyId didn't like that strange environment.
Load the app using RAILS_ENV=production and it worked fine.
Not a solution, but at least could move on...

Resources