是否可以在gsub表达式中使用否定匹配?我希望替换字符串,hello 除了那些开头的字符串hello Peter
my-string.gsub(/^hello@/i, '')
Run Code Online (Sandbox Code Playgroud)
我该@怎么做而不是?
我正在使用Postmark来处理我的Rails 3应用程序中的所有电子邮件,使用postmark-rails gem.
偶尔用户会引入错误的电子邮件或不存在的电子邮件,最终会给予硬弹跳.邮戳引发Postmark::InvalidMessageError错误以处理此问题,我的用户收到的错误是非描述性500错误.
我想将这些错误处理到我的响应界面中,我想知道什么是最好的策略.我现在有几个邮件已经有几十个,所以我不想begin-raise为所有这些方法添加块.将这种开始加注添加到控制器似乎也不是最优雅的解决方案.
我一直在阅读有关向rescue_from我添加块的内容ApplicationController,但后来我不知道如何在界面中处理这个(可能是通过调用使用errors方法的方法?)
我想在开始管道之前听听你的想法.
有任何想法吗?
我有一个Post模型:
class Post < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
# I WANT THIS FUNCTION EXECUTED ON index1
def self.search1(query)
__elasticsearch__.search(
{
query:
}
)
end
# I WANT THIS FUNCTION EXECUTED ON index2
def self.search2(query)
__elasticsearch__.search(
{
query:
}
)
end
index_name "index1"
# I NEED ANOTHER INDEX ? HOW CAN I DO ?
settings index1: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :title, analyzer: 'english'
end
end
end
Post.__elasticsearch__.client.indices.delete index: "index1" rescue nil
Post.__elasticsearch__.client.indices.create index: "index1", …Run Code Online (Sandbox Code Playgroud) 我有一个 Ruby on Rails 4.2 多租户应用程序,我希望每个租户使用它自己的会话 cookie 和 cookie 选项。我尝试了不同的方法,但无法让每个细节都正确。
为了添加更多上下文,在中间件中从请求中的主机 URL 中选择租户,并将其设置为我可以在堆栈中访问的线程变量。我们将该设置对象称为Settings.current_account。
选项 1:中间件:
class AccountableSessionCookies
def initialize(app)
@app = app
end
# @param [Hash] The Rack environment.
def call(env)
dup.call!(env)
end
# @param env [Hash] The Rack environment.
def call!(env)
env['rack.session.options'][:key] = Settings.current_account.session_key
env['rack.session.options'][:expire_after] = Settings.current_account.session_expiration
@app.call(env)
end
end
Run Code Online (Sandbox Code Playgroud)
然后在初始化程序中
Rails.application.config.session_store :cookie_store, key: 'bla',
expire_after: 1_209_600
Run Code Online (Sandbox Code Playgroud)
结果:这将允许我设置expire_after,但不能设置 cookie 名称,该名称将保留bla。
选项 2:自定义 cookie 存储
module ActionDispatch
module Session
class AccountCookieStore < CookieStore …Run Code Online (Sandbox Code Playgroud) cookies ruby-on-rails multi-tenant rack-middleware ruby-on-rails-4
我正在尝试在Rails3.1应用程序中实现我的路由的完全国际化.我已经在使用Francesc Pla的rails-translate-routes来本地化路由动作和资源.最后一步是能够为我的一些模型转换slu ..
要翻译的路线:
http://myhost.com/products/pharmacy --> http://myhost.com/productos/farmacia
Run Code Online (Sandbox Code Playgroud)
我有一个表格的嵌套路线
# routes.rb
match 'products/:category_slug' => "products#index"
Run Code Online (Sandbox Code Playgroud)
我有一个带有实例的模型类别,#<Category id: 1, slug: "pharmacy">我find_by_slug在ProductsController中做类别.
有关如何翻译的任何想法都可以翻译:category_slug路线的一部分吗?
我在MySQL Workbench中构建了一个EER模型,我转发工程师来创建数据库.正向工程完美运行,数据库按照预期从图中创建.
除了表格之外,还有一些我已经包含在模型中的存储过程(又名例程).一旦设置了数据库,这些例程就只能运行一次.它们会自动将必要的数据插入表中.
我的问题是,一旦创建了表,我怎样才能让正向工程过程自动调用/执行其中一个例程.
目前,我必须转发工程师数据库,然后手动调用存储过程?
mysql database-design forward-engineer mysql-workbench eer-model
我有project_mailer布局,但如果project_notification方法有参数,我想使用不同的方法unsubscribe_link = true.
layout "project_mail"
def project_notification(user, projects, unsubsribe_link = false)
attachments.inline['logo_252.png'] = File.read(Rails.root + 'public/images/logo_252.png')
@user = user
@projects = projects
mail(:to => user.email, :subject => "New Projects")
end
Run Code Online (Sandbox Code Playgroud) 当以格式输入格式错误或无效的日期(例如,非le年的2月29日)时,任何验证似乎都将访问nil值。原因是在Rails中进行验证之后,将原始字符串强制转换为日期变量:如果失败(由于上述原因),则强制转换返回nil值,该值存储在模型的属性中。
我发现可以通过这些attribute_before_type_cast函数访问原始日期字符串,因此能够编写以下自定义验证器:
class DateFieldValidator < ActiveModel::EachValidator
def validate_each( record, attribute, value )
# this may be interesting only if value could not be read
return unless value.nil?
# but not if original value was blank
original_value = record.read_attribute_before_type_cast( attribute )
return if original_value.blank?
# ignore any other value but String
return unless original_value.is_a? String
# _parse must return some useful values
parsed_value = Date._parse( original_value )
unless [ :year, :mon, :mday ].all? { |key| parsed_value.has_key?( key ) } …Run Code Online (Sandbox Code Playgroud) 我是ember.js框架的新手,并在rails和ember.debug.js -v 1.10.1(最新版本)上使用ruby.我一直在网上看到ember改变了这个最新的补丁但我无法弄清楚如何解决我的问题:
Uncaught Error: Cannot call `compile` without the template compiler loaded. Please load `ember-template-compiler.js` prior to calling `compile`.
Run Code Online (Sandbox Code Playgroud)
有人可以请我指点让我的编译器正常运行吗?我希望我知道我的项目中的代码片段有助于确定答案......
我在 ruby 模型中有一个方法调用,如下所示:
Contentful::PartnerCampaign.find_by(vanityUrl: referral_source).load.first
Run Code Online (Sandbox Code Playgroud)
在模型spec.rb 文件中,我尝试模拟该调用并通过传入参数来获取值。但我无法弄清楚正确的调用方式。
在我的 spec.rb 文件的顶部,我有:
let(:first_double) {
double("Contentful::Model", fields {:promotion_type => "Promotion 1"})
}
Run Code Online (Sandbox Code Playgroud)
在描述块中,我尝试了以下操作:
expect(Contentful::PartnerCampaign).to receive_message_chain(:find_by, :load, :first).
and_return(first_double)
expect(Contentful::PartnerCampaign).to receive_message_chain(:find_by, :load, :first).with(vanityUrl: 'test_promo_path').
and_return(first_double)
expect(Contentful::PartnerCampaign).to receive_message_chain(:find_by => vanityUrl: 'test_promo_path', :load, :first).
and_return(first_double)
Run Code Online (Sandbox Code Playgroud)
正如您可能猜到的那样,这些都不起作用。有谁知道做这种事情的正确方法?有可能吗?
actionmailer ×2
ruby ×2
cookies ×1
date ×1
eer-model ×1
ember.js ×1
gsub ×1
javascript ×1
mocking ×1
multi-tenant ×1
mysql ×1
postmark ×1
regex ×1
routes ×1
rspec ×1
translation ×1
validation ×1