Undefined local variable or method `searchkick' - ruby-on-rails

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:)

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

undefined method `name' for nil:NilClass with ruby 2.2.2

I've 2 classes with a has_and_belongs_to_many relation.
When I try to destroy an object I get:
> undefined method `name' for nil:NilClass error.
I'm using Ruby 2.2.2. The same code works fine with Ruby 2.1.2.
My controller code:
#cart = Cart.find(1)
#cart.temp_orders.find(4).destroy
My models:
class Cart < ActiveRecord::Base
has_many :temp_orders
end
class TempOrder < ActiveRecord::Base
belongs_to :cart
has_and_belongs_to_many :kids, join_table: :kid_temp_orders
end
class Kid < ActiveRecord::Base
has_and_belongs_to_many :temp_orders
end
Stack trace:
> NoMethodError (undefined method `name' for nil:NilClass): app/controllers/carts_controller.rb:50:in `destroy'
> Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.8ms)
> Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (29.3ms)
> Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (10.7ms
> Rendered /home/dell/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (123.5ms
OP indeed can't post any other data for this error. I think OP doesn't deserve those down votes in this case.
This is an issue with ActiveRecord and Ruby 2.2
You can fix it by switching the ruby version from ruby-2.2.0 to ruby-2.1.2/ruby-2.1.3 or rails version from 4.0.0 to 4.1.2
Check this SO link
upgrading from:
ruby '2.3.0'
gem 'rails', '4.1.1'
to:
ruby '2.3.0'
gem 'rails', '4.1.16'
did the trick for me

Silent ruby/rails failure causes it to consume all memory and crash the server

I have a very strange bug which I'm in need of some clue on. Consider this:
class ApplicationController < ActionController::Base
before_filter :set_timezone
def set_timezone
if logged_in?
Time.zone = current_user.time_zone
end
end
When PayPal tries to send a notification, it comes in like so:
Started POST "/ipn_subscription_notifications" for 173.0.82.126 at 2012-03-15 04:11:45 -0400
Processing by IpnSubscriptionNotificationsController#create as HTML
Parameters: {"txn_type"=>"subscr_signup", etc...
And here it gets hung up. Ruby begins chewing up memory until the machine crashes. This is the fix:
def set_timezone
if current_user
Time.zone = current_user.time_zone
end
end
Let's look at logged_in?:
module AuthenticatedSystem
def logged_in?
current_user ? true : false
end
Which is logically equivalent to the fix.
I suspect an error is being thrown and caught, and somebody is restarting the request process. AuthenticatedSystem is certainly suspect.
This does not happen in the development environment, it throws an error and returns 500:
Started POST "/ipn_subscription_notifications" for 127.0.0.1 at 2012-03-15 15:19:39 -0700
Processing by IpnSubscriptionNotificationsController#create as */*
Parameters: {"foobar"=>nil}
Completed 500 Internal Server Error in 9ms
NoMethodError (undefined method `logged_in?' for #<IpnSubscriptionNotificationsController:0xdfdaaf4>):
app/controllers/application_controller.rb:8:in `set_timezone'
Rendered /usr/local/rvm/gems/ruby-1.9.2-p180#ce2/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
Rendered /usr/local/rvm/gems/ruby-1.9.2-p180#ce2/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered /usr/local/rvm/gems/ruby-1.9.2-p180#ce2/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (4.8ms)
[2012-03-15 15:19:40] ERROR Errno::ECONNRESET: Connection reset by peer
/usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `eof?'
/usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `run'
/usr/local/rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Detecting such failures and handling them gracefully is my goal.
Any ideas? Can I instrument Passenger, or some other part of the Rails stack?
the error is undefined method logged_in? in your IpnSubscriptionNotificationsController, and this controller inherit from ApplicationController, are u sure include AuthenticatedSystem module in your ApplicationController, may be u can try this first
May be this does not resolve your problem, but you should use around_filter :set_timezone instead of before filter. Take a look at this: http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails#working_with_multiple_user_time_zones

Rails3 Engine: Gems (paperclip and inherited_resources) not loaded

In my rails3 engine gemfile, I added:
gem 'paperclip'
gem 'inherited_resources'
I also added this to my gemspec:
s.add_dependency "paperclip"
s.add_dependency "inherited_resources"
I then ran bundle install for both my engine and the client app and started up the console for the client app in order to first test paperclip, to which I get:
Using /Users/ynkr/.rvm/gems/ruby-1.9.2-p180 with gemset rails31beta
ynkr % rails c
/Users/ynkr/.rvm/gems/ruby-1.9.2-p180#rails31beta/gems/actionpack-3.1.0.rc4/lib/action_dispatch/http/mime_type.rb:101: warning: already initialized constant YML
Loading development environment (Rails 3.1.0.rc4)
ruby-1.9.2-p180 :001 > b=Blog::Blog.first
Blog::Blog Load (0.2ms) SELECT `blogs`.* FROM `blogs` LIMIT 1
=> #<Blog::Blog id: 1, user_id: 1, context_id: 2, title: "Cmd Line Blog Title", title_for_url: "cmd-line-blog-title", teaser: "This is the teaser for the command line blog", content: "Some content for the cmd line blog", created_at: "2011-06-28 06:06:55", updated_at: "2011-06-28 06:06:55">
ruby-1.9.2-p180 :002 > b.photos
NoMethodError: undefined method `has_attached_file' for #<Class:0x00000102a57fc8>
from /Users/ynkr/.rvm/gems/ruby-1.9.2-p180#rails31beta/gems/activerecord-3.1.0.rc4/lib/active_record/base.rb:1078:in `method_missing'
from /websites/gems/blog/app/models/blog/photo.rb:6:in `<class:Photo>'
from /websites/gems/blog/app/models/blog/photo.rb:1:in `<top (required)>'
from /Users/ynkr/.rvm/gems/ruby-1.9.2-p180#rails31beta/gems/activesupport-3.1.0.rc4/lib/active_support/dependencies.rb:452:in `load'
OK, so it appears the has_attached_file method from the paperclip gem cannot be found. Why? I'm not sure.
Moving on to inherited_resources, I altered a scaffold generated controller to look like the following:
class Blog::ContextsController < InheritedResources::Base
before_filter :redirect_unless_admin
end
and load up the index page for that to which I am greeted (in the logs) with:
Started GET "/blog/contexts" for 127.0.0.1 at 2011-06-28 12:06:25 -0700
ActionController::RoutingError (uninitialized constant InheritedResources):
Rendered /Users/ynkr/.rvm/gems/ruby-1.9.2-p180#rails31beta/gems/actionpack-3.1.0.rc4/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms)
My question is why are these not working? My hunch is that there is something about engines, namespacing and loading up gems that I am totally unaware of.
You need to require each gem within your engine in an initializer file, eg. lib/<your_engine_name>/engine.rb
Bit more of an explanation over here...
Rails Engine - Gems dependencies, how to load them into the application?

undefined method `parent' for nil:NilClass

I'm getting this strange error using Rails 3.0.2.
ActionView::Template::Error (undefined method `parent' for nil:NilClass):
app/controllers/channels_controller.rb:19:in `index'
This is the controller, and line 19 is the respond_with(#channels) block.
Where do I start to search for errors?
class ChannelsController < ApplicationController
before_filter :set_default_client
respond_to :html, :xml
def index
if params[:cache_set]
#channels = Channel.active.find_all_by_id(params[:cache_set])
else
#channels = Channel.active.find_all_by_id(cookies[:channels].split(','))
end
respond_with(#channels)
end
end
This is the full error:
ActionView::Template::Error (undefined method `parent' for nil:NilClass):
app/controllers/channels_controller.rb:19:in `index'
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (19.8ms)
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (28.6ms)
I'm using Ruby 1.8.7 with Rails 3.0.2.
I've also, just in case, tried Rails 3.0.7 and 3.0.0.
Are you in any case using HAML?
I just bumped into this as well and my colleague (who is not on Stackoverflow yet) found out that it was due to multiline comments in the view template
-#
= helper_method_1
= helper_method_2
I solved the problem by changing HAML version from 3.1.x to 3.0.24.
My new Gemfile looks like this.
gem "rails", "3.0.2"
gem "haml", "3.0.24"
gem "compass", "0.10.6"
It looks like HAML-edge has fixed this problem.
I expect the next release of HAML after 3.1.1 to resolve this.

Resources