我正在使用rspec请求来测试需要在每个请求的标头中使用api-key的JSON API.
我知道我可以这样做:
get "/v1/users/janedoe.json", {}, { 'HTTP_AUTHORIZATION'=>"Token token=\"mytoken\"" }
Run Code Online (Sandbox Code Playgroud)
但是,对每个请求执行此操作都很繁琐.
我已经尝试request.env在前面的块中设置,但是我得到了no method NilClass error自从请求不存在.
我需要一些方法,也许在这里spec-helper,全局获取与所有请求一起发送的标头.
我WebView在我的应用程序中使用a ,我必须拦截请求.我目前正在使用以下代码来执行此操作.
public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestProperty("User-Agent", userAgent);
String mime;
if (url.lastIndexOf('.') > url.lastIndexOf('/')) {
String ext = url.substring(url.lastIndexOf('.') + 1);
mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
} else {
mime = "text/html";
}
return new WebResourceResponse(mime, "UTF-8", conn.getInputStream());
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在大多数情况下工作正常,但并非全部.例如,当我尝试登录到Outlook时,它只显示我的电子邮件或密码不正确,我还看到了其他请求被破坏的情况,但如果我删除,一切正常shouldInterceptRequest.
有没有更好的方法,我目前用来拦截请求?
目前,在我的Rails应用程序中通过表单添加URL时,我们有以下内容before_save并validation检查:
def smart_add_url_protocol
if self.website?
unless self.website[/\Ahttp:\/\//] || self.website[/\Ahttps:\/\//]
self.website = "http://#{self.website}"
end
end
end
validates_format_of :website, :with => /^((http|https):\/\/)?[a-z0-9]+([-.]{1}[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$/ix, :multiline => true
Run Code Online (Sandbox Code Playgroud)
但是,这意味着如果我输入表单字段
testing.com
Run Code Online (Sandbox Code Playgroud)
它告诉我,网址无效,我必须投入
www.testing.com
Run Code Online (Sandbox Code Playgroud)
它接受网址
我希望它接受用户是否输入www或http的URL.
我应该在smart_add_url_protocol中添加其他内容以确保添加它,或者这是验证问题吗?
谢谢
我可以在belongs_to关联中使用jsonb字段作为foreign_key吗?有点像:
belongs_to :product, class_name: "Product", foreign_key: "data ->'product_id'"
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Rails 中扩展模型。
模型User使用users数据库中的表,字段为:username, :password。
class User < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)
模型 SuperUser 使用super_users数据库中的表和字段:user_id,:name:
class SuperUser < ActiveRecord::Base
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
我希望SuperUser成为 的扩展,User以便能够做到这一点:
SuperUser.create(:name => "foo", :username => "bar", :password => "foobar")
Run Code Online (Sandbox Code Playgroud)
或者当我获取数据以获得这样的东西时
> s = SuperUser.find 1
> s.username
> "bar"
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么能做到这一点?
我知道
@testable import MyModule
Run Code Online (Sandbox Code Playgroud)
能够MyModule从"测试"(使用"testTarget"构建)模块探索非公共成员MyModuleTests.
我的"非测试"模块中需要相同的功能.不在生产中,只是在调试模式下.
我的问题是:你知道怎么做吗?
并且相关(我认为,更难的问题):背后真正发生了什么魔法@testable?
在Twilio中,当创建出站时,Call我们以后无法添加第三人.但是我们可以在使用时使用Conference.电话比会议便宜一点.所以它看起来很奇怪,为什么有人打扰创建出站Calls而不是Conference直接启动?
总结一下:使用Callvs Conference进行"常规"(双人)出站呼叫有什么好处吗?除了小成本因素.
我试图允许API请求指定要在对象上返回的字段.我只能使用指定的字段检索对象,但是当它被序列化时,它会抛出一个错误:
ActiveModel::MissingAttributeError (missing attribute: x)
Run Code Online (Sandbox Code Playgroud)
如何实现此功能ActiveModel::Serializer并且可能吗?
我们尝试在将它们合并为一个图像之前对一系列图像应用叠加.现在看起来imagemagick正在将图像转换为应用的颜色而不是应用叠加.关于我们应该采取哪些不同的做法,文件并不十分清楚.如果您对此有任何见解,我将不胜感激.代码如下:
def self.concatenate_images (source, image)
height = FastImage.size(image.url)[0]
width = FastImage.size(image.url)[1]
source = source.first
source = source.resize_to_fill(height, width).quantize(256, Magick::GRAYColorspace).contrast(true)
User.color_variant.each_slice(3).with_index do |slice,variant_index|
slice.each_with_index do |color,color_index|
colored = Magick::Image.new(height, width) { self.background_color = color.keys[0]}
colored.composite!(source.negate, 0, 0, Magick::CopyOpacityCompositeOp)
colored.write("#{User.get_img_path}#{color.values[0]}.png")
if variant_index == 2 && color_index == 0
system "convert #{User.get_img_path}#{slice[0].values[0]}.png #{image.url} +append #{User.get_img_path}#{slice[0].values[0]}.png"
end
if color_index!=0 && variant_index != 3
system "convert #{User.get_img_path}#{slice[0].values[0]}.png #{User.get_img_path}#{slice[color_index].values[0]}.png +append #{User.get_img_path}#{slice[0].values[0]}.png"
end
end
end
Run Code Online (Sandbox Code Playgroud) 我有一个接收大量请求的应用程序,为了减少日志文件大小,我想删除一些日志。
我试图删除的日志之一是:
I, [2015-09-09T19:01:01.372374 #10897] INFO -- : Processing by Api::V1::MyAPIController#show as JSON
Run Code Online (Sandbox Code Playgroud)
基本上,此日志显示请求被路由到哪些控制器操作。我想删除这个日志。是否可以删除此特定日志或更改其日志级别以进行调试。请提供一种删除此日志的方法,因为此日志本身提供的信息很少,但占用了大量的磁盘空间。
感谢帮助,
我正在尝试创建一个博客。
到目前为止,我已经建立了一个名为Blog的名称空间,并以嵌套路由的形式发布了帖子
namespace :blog do
get '', to: 'welcome#index', as: '/'
resources :posts
end
Run Code Online (Sandbox Code Playgroud)
我的问题是,当用户要查看特定帖子时,如何创建HTML并将其保存在数据库中并进行呈现?
换句话说,如何创建,保存和呈现结构化内容?
我有这个功能
application_helper.rb
def nl2br(s)
s.gsub(/\n/, '<br/>')
end
Run Code Online (Sandbox Code Playgroud)
视图
<%= nl2br(@event.rules).html_safe %>
Run Code Online (Sandbox Code Playgroud)
错误:
nil:NilClass的未定义方法'gsub'
有时内容可以是nil这样,最好的处理方式是什么?
当我添加unless s.nil?函数时,我得到了html_safe undefined method error。
我正在我的计算机上开发一个ruby on rails应用程序,我正试图从另一个设备(iPhone,iPad)访问它,以检查一些功能.
如何配置计算机以允许将网页提供给本地网络上的外部设备(我仍处于开发模式).