我试图从控制器中调用image_url
(来自ActionView::Helpers::AssetUrlHelper
模块).我的控制器是一个API控制器,可以呈现json响应.因此,控制器准备对象,我jBuilder
用于呈现实际的JSON响应.这是代码:
class Api::Mobile::HomeController < Api::Mobile::ApplicationController
include ActionView::Helpers::AssetUrlHelper
def index
@items = [Api::Mobile::HomePageItem.new(
type: 'image',
image: image_url("api/mobile/home/tutor-with-student.jpg"))]
end
end
Run Code Online (Sandbox Code Playgroud)
该图像tutor-with-student.jpg
存在于以下文件夹中:
app/assets/images/api/mobile/home/tutor-with-student.jpg
Run Code Online (Sandbox Code Playgroud)
问题是image_url
返回值:
http://myhost.com/images/api/mobile/home/tutor-with-student.jpg
Run Code Online (Sandbox Code Playgroud)
代替
http://myhost.com/assets/api/mobile/home/tutor-with-student.jpg
Run Code Online (Sandbox Code Playgroud)
请注意,当我使用image_url
实际视图时,该方法返回正确的值.
如何image_url
在Controller层上使用该方法以使其返回正确的值?
我正在使用Rails 3.0.5.我使用MySQL作为数据库存储.我有一个模型,其中一列需要是BIGINT.我在创建迁移文件中使用以下内容:
t.column :my_column_name, :bigint
Run Code Online (Sandbox Code Playgroud)
哪个工作正常.
但是,当我跑
rake db:migrate
生成的'schema.rb'文件为特定列创建以下行:
t.integer "my_column_name", :limit => 8
Run Code Online (Sandbox Code Playgroud)
这是不正确的.
我的问题是我错在哪里?为了获得正确的'schema.rb'文件,我应该做些什么吗?我可以更改'schema.rb'文件的生成方式吗?
请注意,'schema.rb'文件错误这一事实会导致我的持续集成服务器出现问题,该服务器运行测试并使用'schema.rb'文件从头开始创建数据库(在运行测试之前).
我使用的是Ruby 1.8.似乎downcase
不会改变非拉丁字符.例如:
"?".downcase
Run Code Online (Sandbox Code Playgroud)
回报 "?"
我知道在Ruby 1.9.1及更高版本中,我可以使用Unicode Utils
(从这里开始).我试过了,它运作正常.返回"?"
上一个示例.
是否存在1.8 Ruby的等效(或任何)解决方案?
我有一个before_filter
在我的ApplicationController
班,我想为它编写一个测试?我应该把这个测试写在哪里?我不想进入每个子类控制器测试文件并重复有关此过滤器的测试.
因此,测试ApplicationController
before_filters 的推荐方法是什么?
请注意,我用Rails 3.2.1
用minitest
.
testing ruby-on-rails before-filter functional-testing ruby-on-rails-3
主要用于测试调试目的,我想知道是否有办法判断一个方法是否被存根。
例如,在我的一项测试中,我写道:
Model.any_instance.stub(:method)
Run Code Online (Sandbox Code Playgroud)
当我有一个真实的实例时,我想写一些类似的东西:
an_instance_of_model.stubbed?(:method) # expecting to return true
Run Code Online (Sandbox Code Playgroud) 我正在使用rspec-rails,我想测试我的邮件程序是否正在渲染正确的视图模板。
describe MyMailer do
describe '#notify_customer' do
it 'sends a notification' do
# fire
email = MyMailer.notify_customer.deliver
expect(ActionMailer::Base.deliveries).not_to be_empty
expect(email.from).to include "cs@mycompany.com"
# I would like to test here something like
# ***** HOW ? *****
expect(template_path).to eq("mailers/my_mailer/notify_customer")
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是一个有效的方法吗?或者我应该做一些完全不同的事情?
更新
MyMailer#notify_customer
可能有一些逻辑(例如,根据客户的区域设置)在不同情况下选择不同的模板。这或多或少与控制器在不同情况下渲染不同视图模板的问题类似。有了RSpec
你就可以写
expect(response).to render_template "....."
Run Code Online (Sandbox Code Playgroud)
它有效。我正在为邮寄者寻找类似的东西。
也许还有其他问题可以解决,但是我似乎无法解决我的问题。
我正在尝试使用ruby 2.2.3将Apple Push Notification Service用作提供程序。
我尝试了一系列发现的宝石,它们都有相同的问题。我尝试过的宝石是:
它们都引发相同的异常:
OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv3 read server session ticket A
Run Code Online (Sandbox Code Playgroud)
这些gem使用OpenSSL创建与APNS端点的SSL连接。但是他们没有做到这一点。
请注意,我正在Mac OS X Yosemite机器上工作。
有什么帮助吗?
编辑有关如何使用的更多信息grocer
:
pusher = Grocer.pusher(
certificate: "/Users/panayotismatsinopoulos/Documents/ProgrammingSwift/certificate.pem",
passphrase: "the passphrase for loading certificate",
gateway: "gateway.sandbox.push.apple.com",
port: 2195,
retries: 3
)
notification = Grocer::Notification.new(
device_token: "....the device token here...",
alert: "Hello There!",
badge: 42)
pusher.push(notification)
Run Code Online (Sandbox Code Playgroud)
我得到的例外是:
OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv3 read server session ticket A
from /Users/panayotismatsinopoulos/.rvm/gems/ruby-2.2.3@my_project/gems/grocer-0.6.1/lib/grocer/ssl_connection.rb:43:in `connect'
from …
Run Code Online (Sandbox Code Playgroud) 我使用 Rails 5 API 模式
路线.rb:
Rails.application.routes.draw do
constraints Basic do
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'
mount Sidekiq::Web => '/sidekiq'
# etc
end
end
Run Code Online (Sandbox Code Playgroud)
约束/基本.rb:
class Basic
def self.matches?(request)
authenticate_or_request_with_http_basic do |email, password|
email == 'foo' && password = 'bar'
end
end
end
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
NoMethodError(Basic:Class 的未定义方法 `authenticate_or_request_with_http_basic')
我可以在约束中使用 http 基本身份验证吗?
有没有办法可以看到 capistrano 任务依赖树?我有一个大项目,有很多任务和依赖项,但我对整个画面一无所知。
帕纳约蒂斯
我有以下情况不符合我的预期,而且,我做错了什么,但我找不到那是什么.该脚本匹配句子中的四个字母单词.我想找到一种迭代匹配组的方法.
x = "This is a statement with four letter words like this"
result = x.match /(\b\w{4}\b)/
=> #<MatchData "This" 1:"This">
Run Code Online (Sandbox Code Playgroud)
不幸的是,$1
包含"This"
,但这就是我得到的.$2
应该包含,"with"
但它是nil
.我究竟做错了什么?为什么$2
nil
?为什么是$n
用n>=2
零?
ruby ×4
rspec ×2
actionmailer ×1
assets ×1
bigint ×1
capistrano ×1
constraints ×1
function ×1
mysql ×1
openssl ×1
regex ×1
rspec-rails ×1
schema.rb ×1
string ×1
testing ×1
utf-8 ×1