I have a controller that calls a model method:
class WelcomeController < ApplicationController
item_num = params[:item_num] || "0001"
#product = Scraper.lookup_item(item_num)
end
Here is the Scraper model:
class Scraper < ActiveRecord::Base
require 'nokogiri'
require 'mechanize'
def self.lookup_item(item_num)
# code goes here
end
end
Why am I getting this error?
NoMethodError: undefined method 'lookup_item' for Scraper:Module
I have run into this error before. grep your project to see if module Scraper is defined anywhere. If it is, remove it, or change it to class instead of module.
Related
Hi i am working on a RoR project with ruby-2.3.0 and rails 4. I am trying to call a method of interactor from controller. My controller is inside the Admin directory as follows:
class Admin::ModeratorsController < Admin::ApplicationController
include Interactor
def index
ModeratorInteractor.find_abc(params)
end
end
My interactor is:-
# frozen_string_literal: true
class ModeratorInteractor
def self.find_abc(params)
User.all
end
end
When i run my code i got an error uninitialized constant Admin::ModeratorsController::ModeratorInteractor.
I also try to include the Interactor:-
include Interactor
Please help how to fix it.Thanks in advance.
You need to define ModeratorInteractor as module to include it in your controller:
module ModeratorInteractor
def self.find_abc(params)
User.all
end
end
Then you need to ensure that the module is loaded properly:
# in application.rb
config.autoload_paths += %W("#{config.root}/lib") # path to your module
Or you can also use require instead of autoload_paths:
require "#{Rails.root}/lib/modeator_interactor"
Then in your controller, you can include it:
include ModeratorInteractor
First, you need to include Interactor in your ModeratorInteractor, also you need to define a call method, not find_abc which will not work and it will throw and error of undefined method, so your final interactor will look like this
# frozen_string_literal: true
class ModeratorInteractor
include Interactor
def self.call
params = context.params
end
end
and you will call it as
ModeratorInteractor.call(params: params)
Voila.
I'm getting an error when trying to call a method in my controller. Following tutorials on how to get this to work but just a bit stuck in the mud and need some help.
NoMethodError in CatalogController#index
undefined method `art' for #<Class:0x007fbe8c338310>
My model
require 'httparty'
require 'json'
class Feed < ActiveRecord::Base
include HTTParty
base_uri 'https://www.parsehub.com/api/v2/runs'
# GET /feeds
# GET /feeds.json
def art
response = self.class.get("/tnZ4F47Do9a7QeDnI6_8EKea/data?&format=json")
#elements = response.parsed_response["image"]
#parsed = #elements.collect { |e| e['url'] }
end
end
My controller
class CatalogController < ApplicationController
def index
#images = Feed.art
end
end
I'm guessing it's something fairly simple I'm forgetting.
def art defines an instance method, not a class method.
You have two options to fix this problem:
1) Make the method a class method by adding self. to the definition:
def self.art
# ...
2) Or create a Feed instance in your controller before calling art:
def index
#images = Feed.new.art
end
I have a Configurable model:
# /models/configuration.rb
class Configuration < ActiveRecord::Base
end
When I reference Configurable in my pages_controller, it works fine:
class PagesController < ApplicationController
def search
#description = Configuration.find_by_name('description') || nil
end
end
But when I reference it in my application_controller.rb, like so
class ApplicationController < ActionController::Base
def get_menu
#menu = Configuration.where(name: 'menu') || nil
end
end
I get the error undefined method 'where' for ActiveSupport::Configurable::Configuration:Class. How can I prevent my Configuration model and ActiveSupport::Configurable::Configuration:Class from colliding like this, or reference my Configuration model directly?
Thanks in advance!
You need to prefix it
::Configuration.where(name: 'menu')
Notice the :: before the class name. They force the interpreter to use the Configuration class in the main namespace, rather than one in the ActiveSupport namespace.
In my rails 4 app I'm having trouble extracting the twitter gem config from my controller to a module, getting
undefined method `include' for #<UsersController:0x007ff7d566df08>
Users_controller.rb
def show
include Twitconfig
...
end
controllers/concerns/Twitconfig.rb
require 'twitter'
module Twitconfig
#client = Twitter::REST::Client.new do |config|
...
end
end
I've tried moving the "include Twitconfig" to out of the new action like so
class UsersController < ApplicationController
include Twitconfig
but that just gave an undefined method error when calling #client.
This is my first time including a module in rails 4 and I've been trying for a while so any help would be really appreciated.
The problem is your module not the way you include it, you cannot write code outside a method.
Include will add instance method to a class, so you should try with :
require 'twitter'
module Twitconfig
def client
client = Twitter::REST::Client.new do |config|
...
end
end
end
And in your controller :
class UsersController < ApplicationController
include Twitconfig
def show
puts "#{client.inspect}
end
It should display your client
I'm learning ruby on rails 4, and want to use concern in ActiveSupport. Here's how I do it:
controllers/concerns/do_things_controller.rb
# file controllers/concerns/do_things_controller.rb
require 'active_support/concern'
module DoThings
extend ActiveSupport::Concern
def do_something
puts 'something'
end
included do
helper_method :do_something
end
end
controllers/application_controller.rb
# file controllers/application_controller.rb
class ApplicationController < ActionController::Base
require 'concerns/do_things_controller'
include DoThings
end
And in views/layouts/application.html.haml I call do_something, it shows error:
undefined method `do_something'
Thanks