% app/decorators/base_decorator.rb require 'delegate' class BaseDecorator < SimpleDelegator def initialize(base, view_context) super(base) @object = base @view_context = view_context end private def self.decorates(name) define_method(name) do @object end end def _h @view_context end end ...
class User::ParameterSanitizer < Devise::ParameterSanitizer def initialize(*) super permit(:sign_up, keys: [:username, :email]) end endAnd then configure your controllers to use it:class ApplicationController < ActionController::Base protected def devise_parameter_sanitizer if resource_class == ...
The last thing to do is to rename the method from ourTweetCreatorclass tocall, and have the class inherit fromApplicationService: # app/services/tweet_creator.rbclassTweetCreator<ApplicationServiceattr_reader:messagedefinitialize(message)@message= messageenddefcallclient =Twitter::REST::Client.newdo|...
Add an instance variable called@hello_messageto the action calledindex, so that the controller looks like this: class HomeController < ApplicationController def index @hello_message = "Welcome to JRuby on Rails on the Sun GlassFish Enterprise Server" end end In Rails, the actions are supposed t...
irb(main):001:0> class Hello #initialize方法是类调用构造函数时执行的。Hello.new("Amy")会调用该方法,将@name赋值为"Amy" irb(main):002:1> def initialize(myname) irb(main):003:2> @name = myname irb(main):004:2> end #set_name是一个实例方法,通过实例(也就是对象)能够调用这个方法,不...
Require the adapter and include Elasticsearch::Model in the class:require 'datamapper_adapter' class Article include DataMapper::Resource include Elasticsearch::Model property :id, Serial property :title, String endWhen accessing the records method of the response, for example, the implementation from...
class MembersController; def rescue_action(e) raise e end; end 到了rails2.2 就变得像下面那样简单了。 require 'test_helper' 总之,没有了明确 require 控制台的源代码的必要,控制台也没有了 Re-raise 所捕捉错误的必要。 但是,Rails2.2 在方式上还存在些许问题。
ActiveRecord没有使用之前提到的开源的connection pool,而是自己实现了一套connection pool,class的层级结构大致如下: classConnectionPool classQueue classReaper 这里的Queue 主要功能是用来存放链接池中创建的connections definitialize(lock=Monitor.new)@lock=lock@cond=@lock.new_cond@num_waiting=0@queue=[]end ...
classGirl<ApplicationRecordattr_accessor:single,:beautifuldefinitialize(params)self.single=params[:single]self.beautiful=params[:beautiful]end# 是否有机会defhave_chance?self.singleend# 是否符合你的口味defsuit_my_taste??self.single&&self.beautifulendend ...
STDOUT/STDERR suppressed for sensitive resource Cookbook Trace: (most recent call first) --- /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/resources/rails_migration.rb:18:in `block in class_from_file' Resource Declaration: --- suppressed sensitive resource output Compiled Resource: --- s...