Why am I getting NoMethodError with my rails app on AppFog? - ruby-on-rails

I have a rails app on AppFog, connected to a mongo database. It is a very simple database, where I used the mongo_mapper gem to create MVC stuff, and then didn't modify it.
rails g scaffold Contacts name:string address:string email:string phone:string -orm mongo_mapper
REALLY basic! Just trying to test out mongo as a database.
I followed the instructions for talking with the database here.
However, every time I try to access the /contacts path, I get a "500 Internal Server Error" message.
The AppFog logs for my app report:
Started GET "/contacts" for 50.193.89.38 at 2013-04-03 21:18:58 +0000
Processing by ContactsController#index as HTML
Completed 500 Internal Server Error in 0ms
NoMethodError (undefined method `collection' for nil:NilClass):
app/controllers/contacts_controller.rb:5:in `index'
The first chunk of my contacts controller file:
class ContactsController < ApplicationController
# GET /contacts
# GET /contacts.json
def index
#contacts = Contact.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #contacts }
end
end
....
Any help would be great.
==UPDATE==
$ rails console
irb(main):001:0> Contact.all
=> []
And using rails s works fine, I don't get the 500 error at all.

The name of the model should be singular, ie Contactand not Contacts.
rails g scaffold Contact name:string address:string email:string phone:string -orm mongo_mapper
The way you created, makes the name of the model be Contacts and not Contact. So when you try Contact.all you get nil.

Related

Rails 5, Ckeditor and Paperclip error when loading and searching for images

I'm using rails 5 api and am trying to implement a ckeditor wysiwyg in a form using angularjs 1. I'm using rails 5.1.4, ckeditor 4.2 and paperclip 5.1.0.
I have followed the directions from here: https://github.com/galetahub/ckeditor for the gem implementation and have run the generator:
rails generate ckeditor:install --orm=active_record --backend=paperclip
When trying to "Browse Server" or upload an image to the server through the ckeditor I am performing the following Get request:
Started GET "/ckeditor/pictures?CKEditor=js-ckeditor&CKEditorFuncNum=1&langCode=en" for 172.23.0.1 at 2017-10-24 01:59:04 +0000
And I then get the following error:
ActionController::RoutingError (undefined method `layout' for Ckeditor::ApplicationController:Class):
ckeditor (4.2.4) app/controllers/ckeditor/application_controller.rb:2:in `<class:ApplicationController>'
ckeditor (4.2.4) app/controllers/ckeditor/application_controller.rb:1:in `<top (required)>'
This is the controller where the error is coming from: https://github.com/galetahub/ckeditor/blob/master/app/controllers/ckeditor/application_controller.rb
Edit: Turns out the rails 5 api does not include ActionView::Layouts out of the box, so I added this following to my ApplicationController:
include ::ActionView::Layouts
This took care of the undefined method 'layout'. However, now I have another error coming from the same ckeditor controller after the Get request has been passed to the index action. I am also using devise_token_auth and pundit for authorization and authentication.
Processing by Ckeditor::PicturesController#index as HTML
Parameters: {"CKEditor"=>"js-ckeditor", "CKEditorFuncNum"=>"1", "langCode"=>"en"}
Completed 500 Internal Server Error in 149ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `ckeditor_authorize!' for #<Ckeditor::PicturesController:0x007f5e7ac7dc30>):
this is the CKeditor Application Controller where you get the error, you should include this code to your question with link to the page
class Ckeditor::ApplicationController < Ckeditor.parent_controller.constantize
layout Ckeditor.controller_layout
before_action :find_asset, only: [:destroy]
before_action :ckeditor_authorize!
before_action :authorize_resource
protected
def respond_with_asset(asset)
asset_response = Ckeditor::AssetResponse.new(asset, request)
if asset.save
render asset_response.success(config.relative_url_root)
else
render asset_response.errors
end
end
end
I don't know why it is not working and I don't have any idea what Ckeditor.controller_layout is.. keep me updated with your progress

Elasticsearch::Transport::Transport::Errors::BadRequest error heroku

I have a rails app which has search system which depends on elasticsearch, i have pushed it on heroku using the bonsai addon, but whenever i try to search something on my app it give me this error in the log.
2017-07-16T04:04:44.083489+00:00 app[web.1]: Completed 500 Internal Server Error in 18ms (ActiveRecord: 1.9ms)
2017-07-16T04:04:44.084229+00:00 app[web.1]: app/controllers/search_controller.rb:7:in `show'
2017-07-16T04:04:44.084222+00:00 app[web.1]: Elasticsearch::Transport::Transport::Errors::BadRequest ([400] {"error":{"root_cause":[{"type":"parsing_exception","reason":"no [query] registered for [filtered]","line":1,"col":22}],"type":"parsing_exception","reason":"no [query] registered for [filtered]","line":1,"col":22},"status":400}):
My Elasticsearch Controller
class SearchController < ApplicationController
before_action :beautify_url
layout "simple"
def show
#post_records = Post.search(query_term).paginate(page: params[:page]).records
#posts = #post_records.to_a.select { |post| post.published? }
#users = User.search(query_term).records.to_a
#tags = Tag.search(query_term).records
end
def users
#users = User.search(query_term).records.to_a
end
private
def beautify_url
if params[:search].present?
case params[:action]
when "show"
redirect_to search_url(q: params[:search][:q])
when "users"
redirect_to search_users_url(q: params[:search][:q])
end
end
end
def query_term
params[:q] || ''
end
end
Please help!!
Bonsai support here. Bonsai clusters are currently being provisioned on Elasticsearch 5.x, and as of Elasticsearch 5.0, the filtered query has been removed. Attempting to use a filtered query in 5.x results in that error message you're seeing.
From what you've shared, I would say the most likely issue is that the client is using a deprecated version of the Query DSL. That would suggest an incompatible gem version.
You can check what version your Elasticsearch gems are by running this from a command line:
bundle show | grep elasticsearch
If they are not 5.x.x, update them in your Gemfile:
gem "elasticsearch", "~> 5"
gem "elasticsearch-rails", "~> 5"
And run bundle update elasticsearch elasticsearch-rails. Push the change to Heroku and try your search again.
If that doesn't help, shoot an email to support#bonsai.io and we'll help you sort it out.

heroku undefined method empty? when upgrading my app to ruby 2.0

I've recently upgraded my Heroku app from Cedar-10 to Cedar-14 with no problems (still using ruby 1.9.3). Then I tried upgrading my app to use ruby 2.0.0-p645 and push it to the heroku server. When I do this I can no longer access my app and I get the following error in the logs;
2015-07-09T12:27:37.480991+00:00 app[web.1]:
2015-07-09T12:27:37.480996+00:00 app[web.1]: NoMethodError (undefined method `empty?' for nil:NilClass):
2015-07-09T12:27:37.480998+00:00 app[web.1]: app/controllers/wines_controller.rb:18:in `index'
2015-07-09T12:27:37.480999+00:00 app[web.1]:
2015-07-09T12:27:37.481001+00:00 app[web.1]:
2015-07-09T12:27:37.481462+00:00 app[web.1]: Processing by WinesController#index as HTML
2015-07-09T12:27:37.481465+00:00 app[web.1]: Completed 500 Internal Server Error in 100.1ms
If I look at line 18 of the wines_controller I have the following;
respond_to do |format|
format.html
format.json {render json: #wines.as_json}
end
I thought it was something in my index.html, so I took that back to the this;
%h1 Wines
With just this one line in my index.html.haml it still had a problem.
If I try and access wines.json this works and gives me a list of my wines. Running on my development box using 2.0.0 works fine and all my tests pass.
Update: Add controller & more info on index.html
Here is my wines controller
def index
# Search via Ransack
#q = current_user.wines.includes(:wine_rack).unconsumed.order("LOWER(winery)").search(params[:q])
#wines = #q.result.page params[:page]
#total = #q.result.sum(:qty)
respond_to do |format|
format.html
format.json {render json: #wines.as_json}
end
end
In regards to my index.html.haml file I uploaded a version of my project which only included this one line;
%h1 Wines
There are no loops happening in the view and I still get the error.
Line 18 refers to the following line in my controller;
respond_to do |format|
I found the answer. I had to upgrade my newrelic_rpm. It's the only thing I've changed and now it works.
- newrelic_rpm (3.5.0)
+ newrelic_rpm (3.12.1.298)
I tested by loading up another instance of heroku and sending my app there a few times with various bits of the controller & view cut out, nothing changed until I remarked out newrelic_rpm gem and it started working. So I upgraded it instead of removing the gem and now it works.
The only reason I removed it was due to me looking for differences between development and production. It was marked as a production only gem in my Gemfile.

Undefined local variable or method `searchkick'

I'm trying to add Searchkick to my Rails application. I'm following the exact instructions on the Get started page but I keep getting the following error:
Started GET "/airports" for 10.0.2.2 at 2014-05-26 10:20:33 +0000
Processing by AirportsController#index as HTML
Completed 500 Internal Server Error in 26ms
NameError (undefined local variable or method `searchkick' for #<Class:0xb48f3ba8>):
app/models/airport.rb:2:in `<class:Airport>'
app/models/airport.rb:1:in `<top (required)>'
app/controllers/airports_controller.rb:3:in `index'
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1#global/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1#global/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1#global/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /home/vagrant/.rvm/gems/ruby-2.1.1#global/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (19.8ms)
Here's what the Airport model looks like:
# ./app/models/airport.rb
class Airport < ActiveRecord::Base
searchkick
end
And this is what the Airport controller looks like:
# ./app/controllers/airports_controller/rb
class AirportsController < ApplicationController
def index
#airports = Airport.all
end
end
What's causing this?
Restart the server. The server has to be restarted after a change is made to the Gemfile.
Other things you can try if that doesn't work:
add gem 'searchkick' to the Gemfile
bundle install
Same here. I checked gem list, searchkick was there.
But I deleted searchkick in Gemfile and runned bundle install. Then add searchkick and bundle install again, it works well:)

heroku error - NoMethodError (undefined method `updated?'

I've a rails 3.1 app on Heroku, I keep getting an 500 error in production which I can't recreate in dev.
When I try to perform an update action in one of my controllers I get a 500. I get the following from heroku logs -
2011-12-05T13:52:35+00:00 app[web.1]: Completed 500 Internal Server Error in 15ms
2011-12-05T13:52:35+00:00 app[web.1]:
2011-12-05T13:52:35+00:00 app[web.1]: NoMethodError (undefined method `updated?' for #<ActiveRecord::Associations::HasOneAssociation:0x00000004058800>):
2011-12-05T13:52:35+00:00 app[web.1]: app/controllers/cars_controller.rb:81:in `block in update'
2011-12-05T13:52:35+00:00 app[web.1]: app/controllers/cars_controller.rb:80:in `update'
The update action on my cars controller is -
def update
#car = Car.find_by_url_identifier(params[:id])
respond_to do |format| #**line 80**
if #car.update_attributes(params[:car]) #**line 81**
CarMailer.gift_confirmation(#car).deliver
format.html { redirect_to(thanks_path(#car.url_identifier)) }
else
format.html { render action: "edit" }
end
end
end
The parameters being posted are -
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"RTXCVvdsKQHc8CxHLeYS9WuztcrI1b4H8SHsdfsKWu+Iz4=",
"car"=>{"name"=>"dgdfsdfsd",
"email"=>"test#test.com",
"recipient_attributes"=>{"name"=>"fdsfsd",
"address1"=>"sdfsdfdsf",
"address2"=>"dsfdsfsdf",
"city"=>"sdfdsf",
"postcode"=>"sdfds"},
"gift_id"=>"2",
"message"=>"fsdfsdfdsf",
"terms"=>"1"},
"commit"=>"submit",
"id"=>"test5"}
My car model looks like this -
class Car < ActiveRecord::Base
validates :name, :presence =>true, :on => :update
validates :url_identifier, :presence =>true, :on => :create
has_one :recipient
belongs_to :gift
accepts_nested_attributes_for :recipient
accepts_nested_attributes_for :gift
def to_param
self.url_identifier
end
end
Any idea how I can fix this? Apart from looking at herokus logs, how else can I debug this?
Oddly the app work for a while if I do a 'heroku restart'
I can't see anything wrong in the code. Since it works on your development machine, I am guessing it is something else. Some difference. Did you run your migrations on heroku?
After deploying on heroku, you have to run your migrations in a separate step. I am not sure if that would give this kind of error. Just guessing here.
I googled the error message. Have you had a look at this thread: http://ruby-forum.com/topic/81719 They had a similar problem and have posted their solution/hack. Might be worth a try.
It looks like you are trying to call the method update to an ActiveRecord::Associations object instead of (probably) the Car model, which is strange because you would think it would be fetched and called on the model if it isn't a method found in Arel.
You could try to debug this by using the production environment settings in your development environment by looking in config/enviroments/production.rb and using those settings for development.
You could also try and recreate the problem on heroku using heroku console

Resources