有没有办法在Rails 3.x中预先填充数据库表和迁移?我有一个状态列表,并且我希望能够在设置项目构建时预先填充它.
我有一个名为python包装器的方法succeed,如下所示:
def succeed(handler, data):
"""Send the given |data| dict as a JSON response in |handler.response|."""
set_headers(handler)
handler.response.write(json.dumps(data))
Run Code Online (Sandbox Code Playgroud)
我试图传递Stripe API调用的结果,使用此方法将信用卡收回另一个服务.这是方法调用,在另一个类中:
succeed(self, dict(success=True, charge_id=charge.id, response=charge))
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我得到'充电不是JSON可序列化'错误.如何使用此代码将所有费用ID响应作为JSON传递?以下是完整的回复:
TypeError: <Charge charge id=ch_103Tsv2kD9PLZlzDG5ce7TE1 at 0x113003b50> JSON: {
"amount": 3500,
"amount_refunded": 0,
"balance_transaction": "xxxxxx",
"captured": true,
"card": {
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"country": "US",
"customer": null,
"cvc_check": "pass",
"exp_month": 5,
"exp_year": 2015,
"fingerprint": "xxxxxxxxxxxxxx",
"last4": "4242",
"name": "stackoverflow@example.com",
"object": "card",
"type": "Visa" …Run Code Online (Sandbox Code Playgroud) 假设我有一个单词列表,如下所示:
['The', 'Quick', 'Brown', 'Fox', 'Jumps', 'Over', 'The', 'Lazy', 'Dog']
Run Code Online (Sandbox Code Playgroud)
我想生成一个列表列表,每个数组包含3个单词,但每个单元都有一个可能的三元组.所以看起来应该是这样的:
['The', 'Quick', 'Brown']
['Quick', 'Brown', 'Fox']
['Brown', 'Fox', 'Jumps']
Run Code Online (Sandbox Code Playgroud)
等等.得到这个结果的最佳方法是什么?
我正在尝试编写一个评论系统,人们可以在其中评论其他评论,并在页面上显示为递归线程.(Reddit的评论系统是我正在努力实现的一个例子),但是我对如何实现这样一个不会很慢且计算量很大的系统感到困惑.
我想每个注释都将存储在注释表中,并包含一个parent_id,它将是另一个注释的外键.我的问题在于如何在没有大量查询的情况下获取所有这些数据,然后如何有效地将注释组织到订单中.有没有人对如何最好地实现这一点有任何想法?
我在PHP中编写了一个非常简单的preg_match_all文件:
$fileName = 'A_DATED_FILE_091410.txt';
$matches = array();
preg_match_all('/[0-9][0-9]/',$fileName,$matches);
print_r($matches);
Run Code Online (Sandbox Code Playgroud)
我的预期成果:
$matches = array(
[0] => array(
[0] => 09,
[1] => 91,
[2] => 14,
[3] => 41,
[4] => 10
)
)
Run Code Online (Sandbox Code Playgroud)
我得到了什么:
$matches = array(
[0] => array(
[0] => 09,
[1] => 14,
[2] => 10
)
)
Run Code Online (Sandbox Code Playgroud)
现在,在这个特定的用例中,这是更可取的,但我想知道为什么它与其他子串不匹配?还有一个正则表达式可能会给我我预期的输出,如果是的话,它是什么?
我在Ruby on Rails 3项目中创建了一个嵌套路由.使用以下路由:
resources :companies do
resources :projects
end
Run Code Online (Sandbox Code Playgroud)
现在我可以通过example.com/projects/id或example.com/companies/id/projects/id查看项目控制器的索引方法,但两个页面都以完全相同的方式显示.如何在第二个示例中更改项目页面以仅显示与该公司关联的项目?
我有一个谷歌图表(使用chartkick设置我正在尝试使图表上的日期显得更短.我的数据集看起来像这样:
{"03/01/13" : 3, "03/02/13" : 0, "03/03/13" : 10 } //etc.
Run Code Online (Sandbox Code Playgroud)
但是,当我加载图表时,日期的格式如下:2013年3月1日.如何在我通过日期或其他简短格式时显示日期?在我使用它的页面上,较长的字段看起来非常狭窄.
之前我曾在项目上工作,将字段存储在逗号分隔或管道分隔的字符串中作为字段,这可能代表选项或类似的东西.我想知道这样做是否被认为是错误的数据库设计,应该始终使用关系表,或者有时以这种方式存储数据是否可接受?
我正在寻找重构一些遗留的PHP代码,我知道PDO通过添加预备语句等更安全,但我想知道使用该PDO::query()方法与mysql_query()方法是否有任何安全性好处.在那儿?
所以我有以下重写规则:
RewriteRule ([^/]*)/([^/]*)/([^/]*)$ /index.php?grandparent=$1&parent=$2&page=$3
RewriteRule ([^/]*)/([^/]*)$ /index.php?&parent=$1&page=$2
RewriteRule ([^/]*)$ /index.php?page=$1
Run Code Online (Sandbox Code Playgroud)
但我需要这个不传递给某些子域的索引页面,所以我在此之前有以下重写条件:
RewriteCond %{REQUEST_URI} !^/css/.*$
RewriteCond %{REQUEST_URI} !^/js/.*$
RewriteCond %{REQUEST_URI} !^/admin/.*$
Run Code Online (Sandbox Code Playgroud)
因此,在查找这些目录中的任何文件时(包括可能位于这些目录的子目录中的文件),规则将不适用.但他们仍然在不断改写index.php.
如何为这些目录创建例外?