ActionView::Template::Error: Undefined Method on live server only - ruby-on-rails

This Ruby on Rails problem is a bit vexing, as the code works fine locally but not when I push it onto my live server.
I've added a few int columns into a table using a migration and have a few checkboxes to determine whether items of a certain status are displayed on a certain page, eg:
status\page myitems featureditems saleitems
status1 X x x
status2 x x
This works fine locally in a development environment, but when I push the code to my live server the "add new feature" page fails to load with the following error:
Started GET "/admin/statuses/new" for 86.159.115.236 at 2012-08-19 13:39:12 -0400
Processing by Admin::StatusesController#new as HTML
Rendered shared/_validationerror.html.erb (0.0ms)
Rendered admin/statuses/_form.html.erb (2.1ms)
Rendered admin/statuses/new.html.erb within layouts/application (2.2ms)
Completed 500 Internal Server Error in 4ms
ActionView::Template::Error (undefined method `show_my_items' for #<Status:0xb6c1690>):
20: </tr>
21:
22: <tr>
23: <td align="center"><%= f.check_box :show_my_items %></td>
24: <td align="center"><%= f.check_box :show_latest_items %></td>
25: <td align="center"><%= f.check_box :show_featured_items %></td>
26: <td align="center"><%= f.check_box :show_search_results %></td>
app/views/admin/statuses/_form.html.erb:23:in `block in
_app_views_admin_statuses__form_html_erb__566719799_79558060'
app/views/admin/statuses/_form.html.erb:1:in
`_app_views_admin_statuses__form_html_erb__566719799_79558060'
app/views/admin/statuses/new.html.erb:7:in
`_app_views_admin_statuses_new_html_erb__880262090_94647510'
app/controllers/admin/statuses_controller.rb:31:in `new'
In this case 'show_my_items' is one of the fields attached to a status, (represented by page1).
Additionally, on live I can edit a status using the checkbox (but it's int datatype...), but on live, though I can view the edit page and submit without an error, it doesn't actually update the field.
So the columns exist in the database, the code is the same code as I have on live, but the error only happens on the live server. Same version of Rails on both (3.1.0.rc8), ruby versions slightly different (local: 1.9.2p320, live: 1.9.3p194).
No idea what's going on...

Just to be sure, did you run the migration on your server in production mode?
$ RAILS_ENV=production rake db:migrate

Well the problem seems to be that Passenger did not start correctly. Either it takes longer than I thought or something else went wrong, but having restarted passenger again it worked fine.

Related

redmine upgrade to 3.4.4 not working [ActionView::Template::Error (undefined method `default_users_time_zone')]

Every thing worked fine for me with Redmine 3.3. Now I wanted to upgrade to 3.4.4 and Redmine is no longer running. I searched the internet but did not find the exact same problem. Similar problems where caused by plugins but I do not have any plugins installed. I use the Passenger Docker image of Redmine.
The logs give me the following error message:
Processing by WelcomeController#index as HTML
Current user: anonymous
Redirected to http://0.0.0.0/login?back_url=http%3A%2F%2F0.0.0.0%2F
Filter chain halted as :check_if_login_required rendered or redirected
Completed 302 Found in 60ms (ActiveRecord: 3.7ms)
App 179 stdout:
Started GET "/login?back_url=https%3A%2F%2Fredmine.mydomain.tld%2F" for 95.90.204.176 at 2018-03-14 19:10:26 +0000
Processing by AccountController#login as HTML
Parameters: {"back_url"=>"https://redmine.mydomain.tld/"}
Current user: anonymous
Rendered account/login.html.erb within layouts/base (24.1ms)
Completed 500 Internal Server Error in 219ms (ActiveRecord: 4.3ms)
ActionView::Template::Error (undefined method `default_users_time_zone' for #<Class:0x00000000055f2120>
Did you mean? default_users_hide_mail):
11: <%= favicon %>
12: <%= stylesheet_link_tag 'jquery/jquery-ui-1.11.0', 'application', 'responsive', :media => 'all' %>
13: <%= stylesheet_link_tag 'rtl', :media => 'all' if l(:direction) == 'rtl' %>
14: <%= javascript_heads %>
15: <%= heads_for_theme %>
16: <%= call_hook :view_layouts_base_html_head %>
17: <!-- page specific tags -->
app/models/user_preference.rb:44:in `initialize'
app/models/user.rb:949:in `pref'
app/helpers/application_helper.rb:1451:in `javascript_heads'
app/views/layouts/base.html.erb:14:in `_app_views_layouts_base_html_erb__2446195152155203147_36658300'
lib/redmine/sudo_mode.rb:63:in `sudo_mode'
What could that be? I need to upgrade to 3.4 because I want to use a plugin but the plugin requires version 3.4 in use.
It was my fault! I did not copy every new settings file to my mounted config directory.

Intermittent behaviour with delete method

In my view I have this link;
<%= link_to 'Delete', post, data: {confirm: 'Are you sure?'}, method: :delete, class: 'btn btn-primary btn-xs' %>
which almost always goes to the delete method in the posts controller, as expected. However, very occasionally, I see this type of error in the logs:
GET "/posts/1252" for 144.132.171.93 at 2016-07-16 04:57:39 +0000
2016-07-16T04:57:39.885823+00:00 app[web.1]: [Rollbar] Scheduling item
2016-07-16T04:57:39.885838+00:00 app[web.1]: [Rollbar] Sending item
2016-07-16T04:57:40.041396+00:00 app[web.1]: [Rollbar] Got unexpected status code from Rollbar api: 400
2016-07-16T04:57:40.041414+00:00 app[web.1]: [Rollbar] Response: {
2016-07-16T04:57:40.041416+00:00 app[web.1]: "err": 1,
2016-07-16T04:57:40.041417+00:00 app[web.1]: "message": "access token required"
2016-07-16T04:57:40.041418+00:00 app[web.1]: }
2016-07-16T04:57:40.041442+00:00 app[web.1]: [Rollbar] Details: https://rollbar.com/instance/uuid?uuid=ded45ccf-11be-4dfe-af4e-775445b9230e (only available if report was successful)
2016-07-16T04:57:40.042290+00:00 app[web.1]:
2016-07-16T04:57:40.042301+00:00 app[web.1]: AbstractController::ActionNotFound (The action 'show' could not be found for PostsController):
In the above case, the post with id 1252 does exist. It looks to me that in these cases, the method: :delete is being ignored and instead of deleting the post, it is trying to show the post. I have tried putting
<%= csrf_meta_tags %>
in the head, but this does not work.
I have been able to trigger this on a staging server using a selenium test and when it happens, the confirm script is not triggered. This suggest to me it is something to do with the page occasionally not loading everything that it needs to interpret this link. How do I fix this?

Impossible to delete a flash

I made a mistake before migrating a plugin, and have written
flash[:notice] = :label_presta_added
instead of
flash[:notice] = l(:label_presta_added)
I corrected my mistake but it seems that my Redmine Plugin has trashed my Redmine. Even though I delete my plugin a migrate once again, I still get this error:
Started GET "/" for 127.0.0.1 at 2016-06-01 22:21:37 +0200
Processing by WelcomeController#index as HTML
Current user: admin (id=1)
Rendered welcome/index.html.erb within layouts/base (28.1ms)
Completed 500 Internal Server Error in 366ms (ActiveRecord: 116.0ms)
ActionView::Template::Error (undefined method `html_safe' for :label_presta_added:Symbol
Did you mean? html_safe?):
97: <div id="sidebar">
98: <%= yield :sidebar %>
99: <%= view_layouts_base_sidebar_hook_response %>
100: </div>
101:
102: <div id="content">
103: <%= render_flash_messages %>
app/helpers/application_helper.rb:312:in `block in render_flash_messages'
app/helpers/application_helper.rb:311:in `render_flash_messages'
app/views/layouts/base.html.erb:100:in `_app_views_layouts_base_html_erb__4104276684161420982_39604440'
lib/redmine/sudo_mode.rb:63:in `sudo_mode'
Can somebody give me a hand here?
Thanks in advance!
This is stored in your session, so usually changing the session secret key will invalidate all sessions and discard any old session data.
You can also try and rescue to clear it out as a one-time deal.
Have you restarted the server? Or you can use flash[:notice] = nil to remove it.
It looks like it throws a html_safe error. Can you see if the method which is rendering the flash is using html_safe? It looks like its coming from there.
Not sure exactly, may be shooting in the dark.
But read these and try may be:
actionview::template::error(undefined method 'html_safe' for nil:NilClass)
http://www.redmine.org/issues/8477

Modify Rails controller to work with Postgres/Heroku

I'm working my way through the railstutorial whilst working on my own mini project. Whilst it has been suggested that the development database you use should be the same as the one in production, the guide uses sqlite for development
and I've been doing the same.
I've deployed some changes recently, that work locally, to my app hosted on Heroku however, when trying to access a specific page, I receive the following error:
If you are the application owner check the logs for more information.
How can I access the logs that are mentioned?
I'm guessing a possible cause is due to the code in my controller that returns data from the db, and works in sqlite but is possibly invalid for postgres. Can someone please suggestion improvements for the controller.
I've attached my code below.
accounts_controller.rb:
# GET /accounts/1/history
def history
#account = Account.find(params[:id])
#accountHistory = Account.select("accounts.*, balances.*").joins("JOIN balances").where('balances.account_id = :accountId', {accountId: #account.id}).where('accounts.id = :accountId', {accountId: #account.id}).order('date desc')
end
history.html.erb
<tbody>
<% #accountHistory.each do |a| %>
<tr>
<td><%= date_format(a.date) %></td>
<td class='account-balance'><%= number_to_currency(a.balance, unit: "£") %></td>
</tr>
<% end %>
</tbody>
UPDATE:
Looking at the Heroku logs, it looks like there's a syntax error for Postgres. Can anyone suggest how to improve it?
heroku logs:
2015-12-22T16:54:39.361012+00:00 app[web.1]: Started GET "/accounts/3/history" for 2.220.200.172 at 2015-12-22 16:54:39 +0000
2015-12-22T16:54:39.370975+00:00 app[web.1]: Account Load (0.9ms) SELECT accounts.*, balances.* FROM "accounts" JOIN balances WHERE (balances.account_id = 3) AND (accounts.id = 3) ORDER BY date desc
2015-12-22T16:54:39.371012+00:00 app[web.1]: LINE 1: ...ounts.*, balances.* FROM "accounts" JOIN balances WHERE (bal...
2015-12-22T16:54:39.371010+00:00 app[web.1]: PG::SyntaxError: ERROR: syntax error at or near "WHERE"
2015-12-22T16:54:39.371013+00:00 app[web.1]: ^
2015-12-22T16:54:39.371015+00:00 app[web.1]: : SELECT accounts.*, balances.* FROM "accounts" JOIN balances WHERE (balances.account_id = 3) AND (accounts.id = 3) ORDER BY date desc
2015-12-22T16:54:39.371797+00:00 app[web.1]: Rendered accounts/history.html.erb within layouts/application (2.3ms)
2015-12-22T16:54:39.371929+00:00 app[web.1]: Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.6ms)
2015-12-22T16:54:39.374155+00:00 app[web.1]:
2015-12-22T16:54:39.374158+00:00 app[web.1]: ActionView::Template::Error (PG::SyntaxError: ERROR: syntax error at or near "WHERE"
Looks like the Heroku error is related to the joins statement. It needs the on, which you can do a couple ways. To do this in an active relation, just change the joins statement from
.joins("JOIN balances")
To
.joins(:balances)
Note - this part may not be needed anymore
.where('balances.account_id = :accountId', {accountId: #account.id})
Looking at your code a little further, there may be something else you are really after. If accountHistory is a list of balance records, this would work for you....
#account = Account.find(params[:id])
#accountHistory = #account.balances
Or, to potentially do in one query...
#account = Account.includes(:balances).find(params[:id])
The balances collection is referenced on account, I suspect
class Account < ActiveRecord::Base
has_many :balances
end
And, with the foreign key on Balance
class Balance < ActiveRecord::Base
belongs_to :account
# Add a scope if you like to reuse code
scope :order_by_date, -> { order(:date => :desc) }
end
You can then reference balances easily. This is where Rails helps you out a lot to make things very readable.
#account.balances.order_by_date
You can assign this to #accountHistory if you want, or just refer to balances from the collection off account in the view which might look like:
<tbody>
<% #account.balances.order(:date => :desc).each do |b| %>
<tr>
<td><%= date_format(b.date) %></td>
<td class='account-balance'><%= number_to_currency(b.balance, unit: "£") %></td>
</tr>
<% end %>
</tbody>
Did you run heroku run rake db:migrate -a <YOUR HEROKU APP NAME> after you deployed to heroku?
If you did, you can check the logs they are referring to with:
heroku logs --tail -a <YOUR HEROKU APP NAME>
Replace the <YOUR HEROKU APP NAME> with the name on your heroku configs. If you don't know how to find that, go to the heroku.com dashboard and find it under settings.

Rails 3 Occasional Routing Error

I'm running Rails 3.1.1 and getting an odd bug. In development (haven't yet tried pushing to production with it) I'm occasionally getting routing errors in my controller or in my mailer template when it tries to generate a url for a newly created record. This happens even though the record is created successfully and appears to have nothing to do with the record properties (I can recreate a record with the exact same params right after and not get the error, it seems totally random when it happens).
It seems to happen maybe one in 10 times, though I can't say I ever saw an incident of it happening before I added the mailer action.
There's one more potentially complicating factor: I'm using an encryption method to obfuscate the record's id in its URL, but this is otherwise working without a hitch. To do this I adapted the method discussed here
It seems to me like the URL's not generated in time for the link_to call some of the time... But that doesn't make much sense to me. I didn't think race conditions were something I needed to worry about here.
Here are my error logs when this happens in the controller (when the params don't call for an email to be generated):
ActionController::RoutingError (No route matches {:action=>"show", :controller=>"watch_lists", :id=>#<WatchList id: 195, title: "sfdsfd", created_at: "2012-03-19 05:18:46", updated_at: "2012-03-19 05:18:46", public_list: false>}):
app/controllers/watch_lists_controller.rb:72:in `block (2 levels) in create'
app/controllers/watch_lists_controller.rb:56:in `create'
And here's when it happens in the mailer template (when the params do call for an email to be generated before the render command):
Rendered watch_list_mailer/share_notification.html.erb (3.2ms)
Completed 500 Internal Server Error in 113ms
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"watch_lists", :id=>#<WatchList id: 210, title: "sdggsgsg", created_at: "2012-03-19 05:47:17", updated_at: "2012-03-19 05:47:17", public_list: true>}):
20: <% end %>
21: <% end %><br>
22: <br>
23: Here's a link to your WatchList: <%= link_to #wl.title, watch_list_url(#wl) %><br>
24: <br>
25: Enjoy!
26: </p>
app/views/watch_list_mailer/share_notification.html.erb:23:in `_app_views_watch_list_mailer_share_notification_html_erb___1391186431365383285_70156615518000'
app/mailers/watch_list_mailer.rb:12:in `share_notification'
app/controllers/watch_lists_controller.rb:124:in `share_notification'
app/controllers/watch_lists_controller.rb:68:in `block (2 levels) in create'
app/controllers/watch_lists_controller.rb:63:in `each'
app/controllers/watch_lists_controller.rb:63:in `block in create'
app/controllers/watch_lists_controller.rb:56:in `create'
EDIT: Upon further testing, this appears to happen regardless of whether I include the mail task. It seems most likely spurred by the obfuscation of the links. It's possible that the encoding of the links has something to do with it (I had to make sure to URI-escape them to prevent slashes in the wrong places elsewhere in my code). I'll investigate this futher and report back.
It was a problem with the id encryption creating invalid links occasionally and me failing to account for that in early enough in the process.
In lib/obfuscate.rb
def uri_encrypt(value)
URI.escape(self.encrypt(value), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end
In my model
def to_param
uri_encrypt(id)
end

Resources