I am trying to create a custom form builder that generates a span with an error message. I keep getting the message
NoMethodError at /class/create
undefined method `content_tag' for #<Padrino::Helpers::FormBuilder::StandardFormBuilder:0x00000005aa24b8>
Here is my extension:
module Padrino
module Helpers
module FormBuilder
class CustomFormBuilder < AbstractFormBuilder
def errors_for(field)
if object.errors[field.to_sym]
error = object.errors[field.to_sym].first
content_tag(:span, error, class: 'error')
end
end
end
end
end
end
end
I have placed this extension in the lib folder.
You should include helper modules you need in your builder class.
module Padrino
module Helpers
module FormBuilder
class CustomFormBuilder < AbstractFormBuilder
include TagHelpers
include FormHelpers
include AssetTagHelpers
include OutputHelpers
...your methods here...
end
end
end
end
Related
I create a route for each product from database with following code:
Products.all.each do |product|
get "/#{product.title.latinize}"
end
end
class String
def latinize
self
end
end #or with helpers
which raises NoMethodError: undefined method `latinize' for "hello":String.
How to use helpers (or class's extensions as seen here) from dynamically generated routes in Sinatra?
It's probably because you are defining the latinize method after you are generating the routes. Move to above the Product.all section.
I have my routes and helpers defined in external files and included by Sinatra, however I'm new to Ruby and I can't now figure out how I can use my helper methods in my routes. When I run the code in RubyMine and access a profile URL I get the error "NoMethodError - undefined method `protected!'"
## Main class
require 'sinatra/base'
class MyApp < Sinatra::Base
register Sinatra::MyHelpers
register ProfileRoutes
...
end
## Helpers include
require 'sinatra/base'
module Sinatra
module LocutusHelpers
def self.registered( app )
app.before do
...
end
def protected!
...
end
end
end
end
## Routes include
require 'sinatra/base'
module ProfileRoutes
def self.registered( app )
app.get '/profile/:userid' do
protected!
...
end
end
end
I've tried def self.protected! for the helper but then it cant access the request object.
I've also tried Sinatra::MyHelpers.protected!, Sinatra.protected! and app.protected!, errors are thrown for all of these too
Do you know how I can access the helpers from my routes? Or have I set something up incorrectly?
It looks like you are mixing up adding helpers from extensions and configuring your app from extensions.
You need to move protected! into a module, then when registering your extension add that module as a helpers module.
module LocutusHelpers
# new module, move protected! into here
module HelperMethods
def protected!
...
end
end
def self.registered( app )
# add new hlpers module
app.helpers HelperMethods
# other extension setup as before...
app.before do
...
end
end
end
I'm trying to do some monkey patching in ActiveShipping UPS class .
I need to add a class level method (starting with .self), so here it's what I'm trying to do:
module ActiveMerchant
module Shipping
class UPS < Carrier
def self.process_request(receiver, sender, packages, options = {})
# some code
end
def regular_method
"foobar"
end
end
end
end
Unfortunately when I'm trying to use it:
ActiveMerchant::Shipping::UPS.process_request(receiver etc)
I get an error:
NoMethodError: undefined method `process_request' for ActiveMerchant::Shipping::UPS:Class
from (irb):6
from C:/Ruby19/bin/irb.bat:19:in `<main>'
There is no class method named process_request in original class.
In original UPS class provided in gem there is one static method defined self.retry_safe = true
and I can use it without errors.
I can also use regular_method after creating instance of UPS class.
More details provided:
I'm working with Rails 2.3 ( :-( ) and Ruby 1.9.2. I have no influce on environment.
Monkey patched code is under plugins/my_plugin/lib/active_shipping/ext/carriers/ups.rb
In /active_shipping I have file named extensions.rb in which i have:
require 'active_shipping'
require_relative 'ext/carriers'
require_relative 'ext/carriers/ups'
It deals with loading everything properly (I suppose basing on regular_method beheaviour from first chunk of code in my question).
I try to invoke process_request in one of my Controllers. This part is little tricky, beacuse i'm using sth like this:
MyModel.courier_service.process_request(parameters)
where courier_service, in this case holds the ActiveMerchant::Shipping::UPS class.
I'm still a newbie in Ruby and don't know what sort of details i should provide.
Maybe you want to do it in another way
File patch_classes.rb:
module ActiveMerchantExpand
module Shipping
module ClassMethods
def self.process_request(receiver, sender, packages, options = {})
# some code
end
end
module InstanceMethods
def regular_method
"foobar"
end
end
def self.included(receiver)
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
end
end
end
Then you have to load your class "ActiveMerchant::Shipping::UPS"
and after that you can attach your methods to your class via
Rails.configuration.to_prepare do
require_dependency [[file for ActiveMerchant::Shipping::UPS]]
require 'patch_classes' )
ActiveMerchant::Shipping::UPS.send(:include, ::ActiveMerchantExpand::Shipping)
end
This is from rails plugin writing, i hope this helps.
regards tingel2k
Do you explicitly require file with your monkey patch? If you just put it under your app or lib path without requiring, it wouldn't load because constant ActiveMerchant::Shipping::UPS is defined in gem and it doesn't trigger dependency resolution mechanism.
I want to use my Sinatra helpers methods in Mustache views.
I do this:
# in app.rb:
...
helpers do
def helloworld
"helloworld!"
end
end
get '/'
mustache :home
end
...
# in views/home
class App < Sinatra::Base
module Views
class Home < Mustache
def hello
helloworld
end
end
end
end
# in home.mustache
<p>{{hello}}</p>
It does not work, I have the error message :
«undefined local variable or method `helloworld' for App::Views::Home:0x000000023ebd48»
How can I use my method helper in Mustache view ?
Or, how can I use my method helper directly from home.mustache ? like this :
# in home.mustache
<p>{{helloworld}}</p>
Many thanks for your help!
You should be able to do something with a module:
# app_helpers.rb
module AppHelpers
def helloworld
"helloworld!"
end
end
# app.rb
helpers AppHelpers
get '/'
mustache :home
end
# views/home.rb
class App < Sinatra::Base
module Views
class Home < Mustache
include AppHelpers
def hello
helloworld
end
end
end
I have a class called User, that needs to include a module.
The class looks like this:
require 'sequel'
require 'modules/validations'
class User < Sequel(..)
many_to_one :country
includes ::Validations
validates_email(:email)
end
The module is defined in a subfolder called modules. It has been added to the $LOAD_PATH and Ruby is no complaining about the loading. The module looks like this:
module Validations
def validates_email(attr, options = {})
email = super.email
end
end
The error I am getting is :
undefined method 'validates_email' for User:class
What am I missing to make this work properly?
If you want to use modules to define class methods you should use the extends method.
class User < Sequel(..)
extend ::Validations
validates_email(:email)
...
end
I suggest reading this article: http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/