我使用的是rails版本3.0.3。如果我在开发环境中运行服务器,一切正常,但是当尝试在生产环境中运行时,rails跳过从/config/ enviroment.rb文件中读取代码。我有以下错误:
=> Booting WEBrick
=> Rails 3.0.3 application starting in production on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/herman/WORK/bloomnet/bronisz
这是一个难以解释的问题。我在另一个模块命名空间中有一个模块,如下所示:
# app/models/points/calculator.rb
module Points
module Calculator
def self.included(base)
base.send(:include, CommonMethods)
base.send(:include, "Points::Calculator::#{base}Methods".constantize)
end
end
end
因此,在其他类中,我需要做的就是:
class U
我有以下设置:
app/models/my_module/service.rb
module MyModule
class Service < ActiveRecord::Base
def self.types
self.subclasses
end
def self.raw_types
self.types.map { |c| c.name.split("::").last }
end
end
end
require_dependency "my_module/service/rack"
在我的Rails项目中,我希望获得嵌套在模块A中的所有模块。
lib/assets/a/b.rb文件包含:
module A
module B
end
end
在Rails控制台中:
A.constants
=> []
A::B.class
=> Module
A.constants
=> [:B]
为什么第一行返回一个空数组,以及如何解决这个问题?
如果我有一条在特定位置查找控制器操作的路线,例如:
things_path GET /things some/things#index
我可以使用以下任何一种方法来满足这一路径:
# app/controllers/some/things_controller.rb
class Some::ThingsController
def index
# do stuff
end
end
# app/controllers/some/things_controller.rb
module Some
class ThingsController
def ind
我曾尝试在rails中使用taglib。我将这段代码(见下文)放在控制器中,但得到了这个错误"uninitialized constant ActivityObjects::BuildController::TagLib“
TagLib::FileRef.open(@activity_object.content_tone_url()) do |fileref|
unless fileref.null?
tag = fileref.tag
title = tag.title
artist = tag.artist
我的应用程序代码的结构如下Rails 3.2所示。如果我进入Rails控制台并输入Foo::Bar::FooBar,它将返回以下警告:
warning: toplevel constant FooBar referenced by Foo::Bar::FooBar
它们所在的应用程序代码和文件:
# app/models/foo/bar/foo_bar.rb
module Foo
class Bar
class FooBar
end
end
end
# app/models/foo/bar.rb
module Foo
class Bar
end
end
#
我想向Rails模型添加一个方法,以便在测试中使用。如果我这样做
class Model
def something_new
do_something
end
end
在Rails控制台或在运行时加载的文件中,Model会被覆盖而不是修改。如果我将类似v = Model.class的代码放在上面的代码行之前,新方法就会成功地添加到现有的类中。显然,引用需要用来表示现有的类正在被重新打开。
另一方面,可以向Fixnum添加一个方法,而不必首先引用它。这里发生了什么,通常是什么方法来确保现有的类被重新打开和修改,而不是被覆盖?
谢谢。
我有一个现在看起来像这样的模型:
class Logo < ActiveRecord::Base
include ImageManipulation
...
end
ImageManipulation是一个名为image_manipulation.rb的库,位于/lib/中。我可以毫无问题地使用该模型,但今天我不得不通过rails控制台进行一些操作,在尝试执行Logo.delete_all时,我收到了以下错误:
NameError: uninitialized constant Logo::ImageManipulation
因此,我将我的模型更改为如下所示:
require &
我在config/initializers/constants/feature_constants.rb中存储了一个常量,称为
NEW_CONTACT_US = 'https://ruby-on-rails.com/contact-us/'
在我的helper方法中,我有一个如下函数:
def link_for_users?
link = if current_user.new_user?
NEW_CONTACT_US
else
OLD_CONTACT_US
end
link
end
我的视图文件如下所示:
= ren
如何告诉Rails (5 beta3)在app/models中而不是在app/models/namespace中查找命名空间模型
我有过
module MyApp
class User < ApplicationRecord
...
end
end
如果我把它放在app/models/myapp中,Rails就会找到它。但是,由于我的所有模型都在MyApp模块中,所以我宁愿将它们保存在app/models中。
谢谢
我有一个类似这样的rails设置。
app/service/TestService.rb
class TestService
def self.doSomething
return 'Hello World!'
end
end
我在控制器中使用这个文件。
require 'TestService'
class IndexController < ApplicationController
def index
@message = TestService.doSomething
end
end
我还在applicat
只是好奇在rails中自动加载是如何工作的
app/models/base_facebook_object.rb
class BaseFacebookObject
end
在我需要它的文件中,
app/models/fb_campaign.rb
class FbCampaign < BaseFacebookObject
end
但是上面的说明不起作用,因为它显示BaseFacebookObject未定义?
在这种情况下,我是否总是需要基本对象文件?
require "#{Rails.root}/app/models/base_facebook_object"
提前感谢
基本上我有这个。
module EcommerceApi
class Application < Rails::Application
end
end
但鲁博科普仍在抱怨:
Use compact module/class definition instead of nested style.Style/ClassAndModuleChildren
在这种情况下,应该如何定义?
我写了一个生成器,它为用户创建了一个包含在他们的模型中的关注点。它在Rails 4.1.8上工作正常,但在Rails 4.2.0上,我得到了这样的消息:
LoadError: Unable to autoload constant Concerns::NameSearch.
NameSearch是生成器创建的关注点模块,app/models/concerns/name_search.rb
module NameSearch
extend ActiveSupport::Concern
included do
...
end
module ClassMethods
我制作了一个新的Rails 4 beta应用程序,并将它部署到Heroku (使用Ruby2.0.0和Unicorn通过Procfile)。
它在本地使用foreman start运行良好,但是当我访问已部署应用程序的主页时,我只得到标准的Heroku 500错误:
We're sorry, but something went wrong.
If you are the application owner check the logs for more information.
但是,在日志中,没有错误。我不知道是什么导致了500人。
这是我上一次git push heroku ma
在我的Ruby on Rails项目中,我在spec/support中有一些文件,它们依赖于同一目录中的其他文件。 #spec/support/page_objects/foo.rb
class Foo
include Bar
end #spec/support/page_objects/bar/bar.rb
module Bar
def hello_world
"Hello World"
end
end 为了让Foo.new.hello_world在规范中工作,我必须显式地在依赖文件前加上require_relative("bar/bar
我得到了下面的dir结构
models/foo/setting.rb
models/foo.rb
foo.rb内容
module Foo
def self.table_name_prefix
'foo_'
end
end
和setting.rb内容
class Foo::Setting < ActiveRecord::Base
end
只要我调用Foo::Setting.find…,我就会得到一个错误SQLException: no such table: settings,它确实是正确的,因为表名为foo_settings,所以rails似乎忽略了为
我有我的rails应用。我有课
class ParserPlugin < ApplicationController
def self.loadPlugins
Dir.foreach("/plugins") do |f|
if f != '.' and f != '..'
load("/plugins" + f.to_s)
end
end
end
def self.createplugin
@@plugins << s
我试图覆盖现有的活动记录模型,但是新的活动记录模型没有来自旧活动记录模型的方法。下面是我试图使用的代码
class ModelA < ActiveRecord::Base
def method_modela_1
logger.info "I am in method_modela_1"
end
def method_modelb_2
logger.info " I am in method_modelb_2"
end
end
接下来,我用ModelA覆盖ModelB
class ModelB < Mo