我正在开发一个处于开发模式的Rails应用程序,它可以注册omniauth.
主持人是
http://localhost:3000/
Run Code Online (Sandbox Code Playgroud)
我正在使用宝石:
gem 'omniauth'
gem 'omniauth-foursquare'
gem 'omniauth-instagram'
Run Code Online (Sandbox Code Playgroud)
当我通过Foursquare注册omniauth时,根本没有问题.所有设置都是正确的,我在Foursquare开发人员设置中的redirect_uri等于主机(localhost:3000)
但是,如果我在Instagram客户端管理器*中填写完全相同的redirect_uri(localhost:3000).Instagram给了我这个:
{
"code": 400,
"error_type": "OAuthException",
"error_message": "Redirect URI does not match registered redirect URI"
}
Run Code Online (Sandbox Code Playgroud)
基于此URL:
https://instagram.com/oauth/authorize?response_type=code&client_id=<ID>&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Finstagram%2Fcallback&state=18415adf24dd97873e61094f67c0fb7a94857fedf93e9d2e&scope=basic
Run Code Online (Sandbox Code Playgroud)
*

根据Instagram,我做错了什么,应该如何解决?
我发起了一个JavaScript/jQuery点击监听器,如下所示:
$("#test").on("click", () => {
console.log("test");
});
Run Code Online (Sandbox Code Playgroud)
这段代码在Firefox中完美运行,但在Chrome中,这似乎给我一个语法错误.这是为什么,因为这对我来说看起来像'ok'语法.
您可以通过操作在控制台中快速测试
var a = () => {return 0;}
a();
Run Code Online (Sandbox Code Playgroud)
在Firefox 27.0.1中,它返回0 In Chrome返回 SyntaxError: Unexpected token )
我在Ruby中有一段代码如下:
def check
if a == b || c == b
# execute some code
# b = the same variable
end
end
Run Code Online (Sandbox Code Playgroud)
这可以写得像
def check
if a || c == b
# this doesn't do the trick
end
if (a || c) == b
# this also doesn't do the magic as I thought it would
end
end
Run Code Online (Sandbox Code Playgroud)
或者以我不需要输入b两次的方式.这是出于懒惰,我想知道.
我正在设置一个可以生成LastFM API请求的应用程序.这些是简单的get请求,我正在使用HTTParty gem.
我的功能如下:
def get_albums
self.class.base_uri "http://ws.audioscrobbler.com/2.0/"
options = {
:user => "Gerard1992",
:method => "user.gettopalbums",
:api_key => Constants::LASTFM_API_KEY,
:format => "json"
}
puts options.to_query
self.class.get "/?#{options.to_query}", {} #options don't work
end
Run Code Online (Sandbox Code Playgroud)
上面显示的这段代码有效.get请求返回一组JSON.我的问题是,这/?#{options.to_query}看起来并不整洁.实际(现在为空{})选项参数也不是.如何让HTTParty选项参数像它应该的那样工作?
这是我尝试过的,但两种情况都失败了:
self.class.get "/", options
self.class.get "/", options => options
Run Code Online (Sandbox Code Playgroud)
我很感激帮助.
这个问题有点奇怪,但我想要的是stub!Rspec 的另一种选择,它不会产生弃用警告.
场景:
我用来stub!在我的帮助器规范中存根某些辅助方法.
例如
stub!(:t_with_partner_lookup).and_return("test")
Run Code Online (Sandbox Code Playgroud)
Rspec比建议使用stub没有感叹号.
所以我写(如建议):
stub(:t_with_partner_lookup).and_return("test")
Run Code Online (Sandbox Code Playgroud)
但是这会产生错误:
Stub :t_with_partner_lookup received unexpected message :and_return with ("test")
Run Code Online (Sandbox Code Playgroud)
在我发现的另一个问题中,我不得不使用helper.前缀.我做了,但它没有删除弃用警告,而是产生了错误.
helper.stub(:t_with_partner_lookup).and_return("test")
Run Code Online (Sandbox Code Playgroud)
生产:
undefined method `t_with_partner_lookup' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3:0x00000103256d50>
Run Code Online (Sandbox Code Playgroud)
我也尝试了这种语法,但产生了与上面提到的相同的错误:
helper.stub(:t_with_partner_lookup){"test"}
Run Code Online (Sandbox Code Playgroud)
用于存根辅助方法的正确语法是什么?
我使用的宝石:
Ruby版本2.1.0
假设我有两种方法的ABC类.
class ABC
def test
"test"
end
def display_test
puts test
end
end
Run Code Online (Sandbox Code Playgroud)
我只希望能够ABC.new.display_test从我的控制台(IRB)打电话(给我'测试')并且无法打电话ABC.new.test或者ABC.new.send(:test)那件事.这可能吗?如果是这样,怎么样?
我有一个像这样的CKeditor图片模型:
class Ckeditor::Picture < Ckeditor::Asset
before_save :set_vars
has_attached_file :data,
:url => "/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:path => ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:styles => { :content => '600>',:medium => '300x300', :quintet => '150x150', :thumb => '118x100#' }
validates_attachment_size :data, :less_than => 2.megabytes
validates_attachment_presence :data
def url_content
url(:content)
end
protected
def set_vars
#self.assetable_id = id
#self.assetable_type = controller_name
end
end
Run Code Online (Sandbox Code Playgroud)
我想要的是在创建新图片期间填充'assetable_id'和'assetable_type'(因为数据库表中有列的列).我想传递它们的变量.就像图片链接到的帖子/事件/用户的id一样.当然,图片被分配给"模型"的类型 - 再次发布/事件/用户.
我不知道这是否是正确的解决方案,但我不知道如何解决它.关于CKeditor gem,配置设置和Rails的在线文档非常糟糕 - 我正在寻找数小时和数小时,我找不到一个与我想要的非常相似的东西 - 所以请帮忙.
我知道如何调整上传的参数,但它们似乎都没有做我想要的事情:
Started POST "/ckeditor/pictures?CKEditor=post%5B14%5D&CKEditorFuncNum=1&
langCode=en&%3Aassetable-id=0&assetable_type=post&
authenticity_token=fsKA68sxkzQpiSMmtcP782i4oI%2FA6KSIsSZuwO5zDWA%3D" for 127.0.0.1 at
2013-04-15 10:05:06 +0200
Processing by Ckeditor::PicturesController#create as HTML
Parameters: {"upload"=>#<ActionDispatch::Http::UploadedFile:0x007f94f80beef8 …Run Code Online (Sandbox Code Playgroud) 我不知道如何用正确的数学术语来称呼它.考虑一个采用两位数的方法:
def num_of_sum(total, group_count)
end
Run Code Online (Sandbox Code Playgroud)
where total是整数,group_count是一个整数.
我怎样才能获得一个"精确"分组的整数数组 - group_count-length总和直到total.
我的规格看起来像:
describe "number to sum of" do
it "grabs all numbers" do
expect(num_of_sum(10, 2)).to eq([5,5])
expect(num_of_sum(10, 3)).to eq([3,3,4])
expect(num_of_sum(20, 3)).to eq([6,7,7])
expect(num_of_sum(100, 3)).to eq([33,33,34])
expect(num_of_sum(100, 2)).to eq([50,50])
end
end
Run Code Online (Sandbox Code Playgroud)
我尝试了这个,它有效:
def num_of_sum(total, in_groups_of)
result = []
section_count ||= (total.to_f / in_groups_of.to_f).round
while(total > 0)
total -= section_count
if (total - section_count) < 0 && (total + section_count).even?
section_count += total
total -= total
end …Run Code Online (Sandbox Code Playgroud) 我有一段像这样的代码.这段代码有效,但看起来很糟糕.
if(typeof(d.object) != "undefined"){
if(typeof(d.object.property) != "undefined"){
if(typeof(d.object.property.length) != "undefined"){
// do the code
}
else alert("error");
}
else alert("error");
}
else alert("error");
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以重写,所以它做同样但更有效.特别是因为错误都是一样的.
假设您在Rails中有一个简单的查询,就像这样
a = 42
Klass.where("`column_1` = ? OR `column_2` = ? OR `column_3` = ?", a, a, a)
Run Code Online (Sandbox Code Playgroud)
这可以更优雅地完成,这样你就不需要输入a, a, a三次了吗?它工作正常,但看起来很糟糕.
假设我有一个数组
array = [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
如何比较第一个值和第二个值,第二个值与第三个值等.
我唯一能想到的就是这个(相当难看)
compared = array.each_with_index.map do |a,i|
array[i+1].nil? ? nil : array[i] - array[i + 1]
end
compared.compact # to remove the last nil value
Run Code Online (Sandbox Code Playgroud)
我想要的是
[-1, -1, -1, -1]
Run Code Online (Sandbox Code Playgroud)
实现这一点有一个很好的"红宝石方式"吗?没有使用所有丑陋array[i]和array[i+1]东西.
我有一个Javascript对象,如下所示:
var a = {
b: [1,2,3,4,5,6,7,8,9],
c: [10,11,12,13,14,15]
}
Run Code Online (Sandbox Code Playgroud)
循环遍历两个数组(我想循环遍历每个值!)我使用$.each,但我可以这样做:
// Option A:
$.each(a, function(i, d){
$.each(d, function(j, e){
console.log(e);
}
});
Run Code Online (Sandbox Code Playgroud)
或这个
// Option B:
$.each(a.b, function(k, f){
console.log(f)
})
$.each(a.c, function(l, g){
console.log(g)
})
Run Code Online (Sandbox Code Playgroud)
两者都生成完全相同的输出,但这两个版本中哪一个更快/更高效/更好?
有什么不同吗?
或者是否有更快/更有效/更好的方式(没有$.each)?
对于我为锻炼而做的练习(扫雷任务),我需要将 an 转换usize为 achar以便将其插入到std::string::String.
用最少的代码行描述问题:
let mut s = String::from(" ");
let mine_count: usize = 5; // This is returned from a method and will be a value between 1 and 8.
s.insert(0, _______); // So I get: "5 " at the underscores I do:
Run Code Online (Sandbox Code Playgroud)
我目前这样做的方式是:
mine_count.to_string().chars().nth(0).unwrap(); // For example: '2'
Run Code Online (Sandbox Code Playgroud)
或者在 rust playground 中查看完整示例。不知何故,这并不让我觉得优雅。
我也试过:
mine_count as char; // where mine_count is of type u8
Run Code Online (Sandbox Code Playgroud)
但是,当添加mine_count …