NoMethodError (undefined method) however class method is defined - ruby-on-rails

I have created a new class Project which is inherited from ActiveRecord::Base. I defined a class method called get_all and I would like to use in Controller but I got NoMethodError (undefined method for ...)
Model:
class Project < ActiveRecord::Base
def self.get_all
find(:all)
end
end
Controller:
class Controller < ApplicationController
unloadable
def index
#projects = Project.get_all
end
end

Note that in rails 3 the find(:all) method ( without any options ) is deprecated in favor of the all method. More about it:
http://m.onkey.org/active-record-query-interface
Also, I don't know why are you making that function, when you could just do:
#projects = Project.all
just like chrisbulmer said.
This should work:
Project model
def self.get_all
Project.all
end

Related

Undefined method in controller

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

Why isn't a module being read by my View-backed Model?

My controller calls the method bar:
class CompsController < ApplicationController
include ApplicationHelper
def quick_create
#var = Matview.bar #projects
end
end
bar is defined in a model that represents a materialized view in my database (it is not in my schema):
class MatView < ActiveRecord::Base
include ApplicationHelper
table_name = 'mat_views'
def self.bar(arg)
foo arg
end
end
'bar' calls method foo, which is defined in my ApplicationHelper:
module ApplicationHelper
def foo(arg1)
#do stuff
end
end
I've included the ApplicationHelper in both my controller and model, and yet I get this error:
NoMethodError in CompsController#quick_create
undefined method `foo' for Matview(Table doesn't exist):Class
why?
Matview.bar #projects
Is calling a class level method on the MatView class.
But your foo and bar are both instance method definitions. To make them class methods, you need def self.bar(arg) or def self.foo(arg1)
And to get class methods into your ActiveRecord model, you need to extend, not include the module:
class MatView < ActiveRecord::Base
extend ApplicationHelper
end
Or, if that does not sound like what you meant to do, then maybe you meant to do:
Matview.new.bar #projects
in which case the instance methods like you wrote them should work.

Helper method not exposed to controller Rails

Here's the code:
class SyncController < ApplicationController
def get_sync
#passed_ids = params[:ids].split(',')
#users = #passed_ids.collect{|id| User.find(id)}
#add the current user to the list
#users << current_user
#recommendations = get_recommendations(#users)
end
end
module SyncHelper
def get_recommendations(users)
users
end
end
I'm getting a can't find get_recommendations method error...
Your SyncHelper module needs to be included into your SyncController class. You can either add the line
include SyncHelper
in your class definition, or, if SyncHelper lives in the expected app/helpers file, you can use the rails helper method.

How to use a custom helper via the "helper" method in Rails 3?

I'm trying to create a custom helper like this:
# app/controllers/my_controller.rb
class MyController < ApplicationController
helper :my
def index
puts foo
end
end
# app/helpers/my_helper.rb
module MyHelper
def foo
"Hello"
end
end
But, I got the following error:
undefined local variable or method `foo' for #<MyController:0x20e01d0>
What am I missing ?
Generally, I do the opposite: I use controller methods as helpers.
class MyController < ApplicationController
helper_method :my_helper
private
def my_helper
"text"
end
end
Helpers are accessed from the views, not the controllers. so if you try to put the following inside your index template it should work:
#my/index.html.erb
<%= foo %>
If you do want to access something from the controller, then you should use the include syntax instead of helper, but do not name it like a helper module in that case.
How about just including the helper as a mixin in the controller...
class MyController < ApplicationController
include MyHelper
def index
puts foo
end
end

define method in model that can be accessed in controller

I have defined a problems method in my Report model. I need to use the value of Report.problem in the report's controller while defining the action show. But i keep getting the error message 'undefined method problem'. How do i solve this? Any assistance would be greatful.
I have a report model and a problem model that contains a list of all problems.
In report model
def problems1
Problem.find(:all, :conditions => )
end
In the reports controller i need something like
def show
#report = Report.problems1
end
you have to assign self.method_name to use as a class method
Follow following rule for Model methods
Class Method
def self.problem
end
in controller
Report.problem
Instance method
def problem
end
in controller
report = Report.new
report.problem
If you define method as class method
class Report < ActiveRecord :: Base
def Report.problem
puts 1
end
end
Report.problem
>1
But if you define method as object
class Report < ActiveRecord :: Base
def problem
puts 1
end
end
This method call
report = Report.new
report.problem
>1

Resources