I have created a concern in controllers/concerns called reports.rb
In it I created the ClassMethods module.. It looks like this
module Reports
extend ActiveSupport::Concern
included do
require 'peddler'
require 'csv'
end
module ClassMethods
def report_details(token, marketplace_id, merchant_id)
#...
end
I included this concern in my Merchants controller like this:
class MerchantsController < ApplicationController
include Reports
And then tried to call the report_details method in an action on the Merchants controller like so:
def pull_from_amazon
me = Merchant.find(params[:merchant_id])
marketplace = me.marketplace
token = me.token
Merchant.report_details(token, marketplace, params[:merchant_id])
redirect_to root_path
end
According to this post Rich on Rails I should be able to call:
Merchant.report_details(xx,xxx,xxxx)
But I get a NoMethodError when I try.. I also tried:
me.report_details(xx,xxx,xxxx)
And got the same NoMethodError
What did I do wrong?
Try this
MerchantController.report_details(xx,xxx,xxxx)
But you should better include this concern in model.
Related
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.
i am beginning to create a module that has shared functionality in the controllers of my rails app. I am trying to include a record find in another controller that requires that record. I use friendly id to slug instances of a corp_page, trying to find the corp_pages record the controllers gives me this error,
ActiveRecord::RecordNotFound at /admin/corporate_pages/home/corporate_panels
Couldn't find CorporatePage without an ID
The code,
module CorporateConcern
extend ActiveSupport::Concern
included do
before_filter :get_corp_page
before_filter :get_corp_panel
end
def get_corp_page
#corp_page = CorporatePage.friendly.find(params[:id])
end
def get_corp_panel
#corp_panel = CorporatePanel.where(id: params[:id]).first
end
end
class Admin::CorporatePanelsController < AdminController
include CorporateConcern
......
end
class Admin::CorporatePagesController < AdminController
include CorporateConcern
end
thanks in advance
I want to define a class and let many helpers use.
I can include MvaasPortal moude in fine,
Then I can new the object , but can not use any methods of the object,
It's so strange.
If I can not use the methods in the object, why I can new the object.
Ruby is so strange.
#portal = Portal.new
There is no methods in #portal object
mvaas_portal.rb
module MvaasPortal
module InstanceMethods
class Portal
def initialize(server_url)
~~~~
end
def query_server(body_to_send={},session_id=nil)
~~~
end
end
end
def self.included(receiver)
receiver.send :include, InstanceMethods
end
end
If you're using rails, you can use ActiveSupport::Concern : http://api.rubyonrails.org/classes/ActiveSupport/Concern.html
If don't, take a look at the first example on the link.
Moreover, your namespace is a little bit weird and misses some context. Here is an example with a dummy method :
require 'active_support/concern'
module MvaasPortal
include ActiveSupport::Concern
def an_instance_method
puts "Here!"
end
end
class Portal
include MvaasPortal
end
Portal.new.an_instance_method
=> "Here!"
Possible Noob Warning: New to RoR
I am trying to use concerns in RoR. Right now I just have a very simple concern writen
#./app/controllers/concerns/foo.rb
module Foo
extend ActiveSupport::Concern
def somethingfoo
puts "Ayyyy! Foo"
end
end
When I try and use this concern in my controller I get a undefined method error
#./app/controllers/foo_controller.rb
class FooController < ApplicationController
include Foo
def show
Foo.somethingfoo # undefined method 'somethingfoo' for Foo:Module
render plain: "Ohh no, It doesnt even show me because of the error above me"
end
end
To my knowledge somethingfoo should be called but it is not. I have also tried defining somethingfoo in a included do ... end block in the concern but this does not work either.
Is there something I am missing? Can concerns not be used like this with controllers?
If you include modules (extended by ActiveSupport::Concern or not), the methods of that module become instance methods of the including class/module.
Your Controller method should hence read
def show
somethingfoo
render plain: "Yeah, I'm shown!"
end
I have a custom plugin (I didn't write it) that is not working on rails 3, however it did work with rails 2. It is for a custom authentication scheme, here is what the main module looks like:
#lib/auth.rb
module ActionController
module Verification
module ClassMethods
def verify_identity(options = {})
class_eval(%(before_filter :validate_identity, :only => options[:only], :except => options[:except]))
end
end
end
class Base
#some configuration variables in here
def validate_identity
#does stuff to validate the identity
end
end
end
#init.rb
require 'auth'
require 'auth_helper'
ActionView::Base.send(:include, AuthHelper)
AuthHelper contains a simple helper method for authenticating, base on a group membership.
When I include 'verify_identity' on an actioncontroller:
class TestController < ApplicationController
verify_identity
....
end
I get a routing error: undefined local variable or method `verify_identity' for TestController:Class. Any ideas how I can fix this? Thanks!
It worked in 2.3 because there was an ActionController::Verification module back there. It's not working in 3.0 because this module doesn't exist. Rather than relying on Rails to have a module that you can hook into, define your own like this:
require 'active_support/concern'
module Your
module Mod
extend ActiveSupport::Concern
module ClassMethods
def verify_identity(options = {})
# code goes here
end
end
end
end
and use:
ActionController::Base.send(:include, Your::Mod)
To make its functions available. ActiveSupport::Concern supports you having a ClassMethods and InstanceMethods module inside your module and it takes care of loading the methods in these modules into the correct areas of whatever the module is included into.