我正在使用Ruby on Rails v3.0.9和jQuery 1.6.我试图从一些网站检索和显示favicon图像.
例如,为了对单个网站执行此操作,我使用以下代码:
$jQ.ajax({
type: 'GET',
url: 'http://www.facebook.com/favicon.ico',
data: '',
error: function(jqXHR, textStatus, errorThrown) {
$jQ('#facebook_favicon').replaceWith('ERROR');
},
success: function(data, textStatus, jqXHR) {
$jQ('#facebook_favicon').replaceWith("<img width='16px' height='16px' src='http://www.facebook.com/favicon.ico' />");
}
});
Run Code Online (Sandbox Code Playgroud)
我使用上面执行HTTP请求的代码来检查是否找到了favicon:如果是(成功的话),它将#facebook_favicon
用刚找到的favicon.ico
图像替换HTML标签的相关内容,否则它将用ERROR
文本替换它.
但是,当我加载放置上述JavaScript的页面并检查Firebug控制台时,我得到以下内容:
这意味着请求已成功执行(HTTP状态代码全部200
,但Gmail除外)但我无法在成功时显示图标图像.为什么?问题是什么?
我注意到在Firebug响应中都是空白的,如下所示:
我能做些什么来完成我想做的事情(我主要是指检查是否找到了网站图标)?
如果我使用如下代码,它将工作,但它不检查favicon存在:
$jQ('#facebook_favicon').replaceWith("<img width='16px' height='16px' src='http://www.facebook.com/favicon.ico' />");
Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby on Rails v3.0.9,我想知道如果我声明一个常量值会发生什么(坏的):
MAX_LENGTH ||= 30
Run Code Online (Sandbox Code Playgroud)
顺便说一句:我正在开发一个"acts_as_something"插件(在我的应用程序中有多个类"acts_as_something"),我必须声明上面的常量值,以便在/.../log/apache2/error.log
(生产模式)中不显示\生成"警告消息",如下所示:
warning: already initialized constant MAX_LENGTH
Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby on Rails 3.0.10,我想检索scheme://domain
通用URL 的一部分(注意:URL语法也可以scheme://domain:port/path?query_string#fragment_id
).
也就是说,例如,如果我有以下网址
http://stackoverflow.com/questions/7304043/how-to-retrieve-the-scheme-domainport-part-of-a-generic-url
ftp://some_link.org/some_other_string
# Consider also 'https', 'ftps' and so on... 'scheme:' values.
Run Code Online (Sandbox Code Playgroud)
我想只检索
# Note: The last '/' character was removed.
http://stackoverflow.com
ftp://some_link.org
# Consider also 'https', 'ftps' and so on... 'scheme:' values.
Run Code Online (Sandbox Code Playgroud)
部分.我怎样才能做到这一点?
我正在使用rails v3.2.2,当我尝试加载相关记录时,我收到一个奇怪的错误.
以下是我得到的终端输入/输出:
1.9.2-p318 :011 > Category.first
=> #<Category id: 1, ...>
1.9.2-p318 :013 > Category.first.articles
Article Load (0.2ms) SELECT `articles`.* FROM `articles` LIMIT 1
(Object doesn't support #inspect)
1.9.2-p318 :014 > Category.first.articles.first
Category Load (0.2ms) SELECT `categories`.* FROM `categories` LIMIT 1
NoMethodError: undefined method `scoped' for Category::Article:Module
from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:123:in `target_scope'
from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/through_association.rb:15:in `target_scope'
from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:87:in `scoped'
from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/collection_association.rb:569:in `first_or_last'
from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/collection_association.rb:101:in `first'
from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/collection_proxy.rb:46:in `first'
from (irb):14
from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in `start'
from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in `start'
from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.2/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in …
Run Code Online (Sandbox Code Playgroud) 我正在使用Rails 3.2.2,我想从数据库中检索记录,其中至少有 3个列值存在.也就是说,如果我有一个具有10个属性(表列)的类(一个MySQL数据库表),其值可以是nil
(null
),那么我想执行一个查询,以便返回其中至少有3个对象(记录)属性值存在(不null
).
可能吗?如果是这样,怎么做?
更新如果我有一个具有10个属性(表列)的类(一个MySQL数据库表),其值可以是nil
(null
)或 " " (not
null
),那么我想执行一个查询,以便至少返回对象(记录)存在这10个属性值中的3个(不是null
而不是" ").