小编Jam*_*mes的帖子

使用浅路由时,不同的路由需要不同的form_for参数

我在这里使用Simple Form,但这也是普通Rails表单的问题.使用浅路由时,form_for需要不同的参数,具体取决于它使用的上下文.

示例:对于编辑(http://localhost:3000/notes/2/edit),_ form.html.erb需要simple_form_for(@note).但是为了创建一个新的音符(http://localhost:3000/customers/2/notes/new)_form.html.erb需要simple_form_for([@customer, @note]).如果接收到错误的参数,我将得到一个找不到方法的错误.

处理这个问题的最佳方法是什么?

  • 我可以制作两种不同的形式,但这看起来很混乱.
  • 我必须为后端链接设置@customer,但我可以在表单中使用不同的变量(比如@customer_form),而不是在编辑和更新方法中设置它,但这是不一致的,有点混乱,因为我必须在新方法中设置@customer_form和@customer.
  • 我可以做这个人做的事情并将表格分成多个文件.它看起来是迄今为止最好的选择,但我并不是很喜欢它,因为你不能只打开_form.html.erb看看发生了什么.

这些是我唯一的选择吗?

示例如下:

配置/ routes.rb中

Billing::Application.routes.draw do
  resources :customers, :shallow => true do
    resources :notes
  end
end
Run Code Online (Sandbox Code Playgroud)

耙路线| grep note

    customer_notes GET    /customers/:customer_id/notes(.:format)         notes#index
                   POST   /customers/:customer_id/notes(.:format)         notes#create
 new_customer_note GET    /customers/:customer_id/notes/new(.:format)     notes#new
         edit_note GET    /notes/:id/edit(.:format)                       notes#edit
              note GET    /notes/:id(.:format)                            notes#show
                   PUT    /notes/:id(.:format)                            notes#update
                   DELETE /notes/:id(.:format)                            notes#destroy
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/笔记/ _form.html.erb

#                      v----------------------------- Right here
<%= simple_form_for (@note), html: { class: 'form-vertical'} do |f| %>
  <%= …
Run Code Online (Sandbox Code Playgroud)

form-for ruby-on-rails-3

42
推荐指数
3
解决办法
7121
查看次数

有一种简单的方法可以在Java中将两列输出到控制台吗?

正如标题所说,有一种简单的方法可以在Java中向控制台输出两列吗?

我知道\t,但在使用printf时,我还没有找到基于特定列的空间方法.

java

32
推荐指数
3
解决办法
7万
查看次数

在Python中传递具有多个返回值作为参数的函数

因此,Python函数可以返回多个值.让我感到震惊的是,如果可能的话,这将是方便的(尽管可读性稍差).

a = [[1,2],[3,4]]

def cord():
    return 1, 1

def printa(y,x):
    print a[y][x]

printa(cord())
Run Code Online (Sandbox Code Playgroud)

......但事实并非如此.我知道你可以通过将两个返回值转储到临时变量中来做同样的事情,但它看起来并不优雅.我还可以将最后一行重写为"printa(cord()[0],cord()[1])",但这会执行两次cord().

有一种优雅,有效的方法吗?或者我应该只看到关于过早优化的引用并忘记这一点?

python return-value function-calls

15
推荐指数
2
解决办法
7906
查看次数

在保留原件的同时传递列表

所以我在教自己Python,我遇到了列表问题.我想传递我的函数列表并在保留原始列表的同时弹出项目.如何使python"instance"成为传递的列表,而不是将指针传递给原始列表?

例:

def burninate(b):
    c = []
    for i in range(3):
        c.append(b.pop())
    return c

a = range(6)
d = burninate(a)
print a, d
Run Code Online (Sandbox Code Playgroud)

输出:[0,1,2] [
5,4,3 ] 期望输出:[0,1,2,3,4,5] [5,4,3]

谢谢!

python list

12
推荐指数
4
解决办法
369
查看次数

在PyQt中打开第二个窗口

我正在尝试使用pyqt在单击QMainWindow上的按钮时显示自定义QDialog窗口.我一直收到以下错误:

$ python main.py 
DEBUG: Launch edit window
Traceback (most recent call last):
  File "/home/james/Dropbox/Database/qt/ui_med.py", line 23, in launchEditWindow
    dialog = Ui_Dialog(c)
  File "/home/james/Dropbox/Database/qt/ui_edit.py", line 15, in __init__
    QtGui.QDialog.__init__(self)
TypeError: descriptor '__init__' requires a 'sip.simplewrapper' object but received a 'Ui_Dialog'
Run Code Online (Sandbox Code Playgroud)

我已经浏览了几个在线教程,但大多数都停止展示如何使用非内置对话框窗口.我使用pyuic4为主窗口和对话框生成了代码.我认为应该是相关的代码如下.我在这里错过了什么?

class Ui_Dialog(object):
    def __init__(self, dbConnection):
        QtGui.QDialog.__init__(self)
        global c
        c = dbConnection

class Ui_MainWindow(object):
    def __init__(self, dbConnection):
        global c
        c = dbConnection

    def launchEditWindow(self):
        print "DEBUG: Launch edit window"
        dialog = QtGui.QDialog()
        dialogui = Ui_Dialog(c)
        dialogui = setupUi(dialog)
        dialogui.show()

class …
Run Code Online (Sandbox Code Playgroud)

python dialog pyqt

11
推荐指数
1
解决办法
3万
查看次数

使用"NOT IN"时非常慢的子查询

我正在为大型预先存在的Access数据库中包含的数据生成报告(紧凑和修复后约500 MB),我遇到了一个慢速子查询的问题.

数据库有一个大表,其中包含每个客户购买的记录.这是一个简单的查询,可以找到购买蓝色小部件的客户.它在几秒钟内完成并返回大约一万条记录.

SELECT DISTINCT CustomerId 
FROM ProductSales
WHERE Product = 'BLUE' 
Run Code Online (Sandbox Code Playgroud)

这是一个查询,试图找到已经购买蓝色小部件但不是红色小部件的客户.运行大约需要一个小时.

SELECT DISTINCT CustomerId FROM ProductSales
WHERE Product = 'BLUE' 
AND CustomerId NOT IN (
    SELECT CustomerId 
    FROM ProductSales 
    WHERE Product = 'RED'
)
Run Code Online (Sandbox Code Playgroud)

有没有办法重构第二个查询,使其花费几分钟而不是一个小时?

ms-access query-optimization

7
推荐指数
1
解决办法
7021
查看次数

提升正则表达式和混淆错误

我正在尝试使用Boost正则表达式来查看其中是否有某个整数.

此页面上的一个示例是

bool validate_card_format(const std::string& s)
{
   static const boost::regex e("(\\d{4}[- ]){3}\\d{4}");
   return regex_match(s, e);
}
Run Code Online (Sandbox Code Playgroud)

还有一个可能是工作示例这里.

但是当我在我的机器上尝试它时,我得到五页不可读的错误.这是怎么回事?

#include <boost/regex.hpp>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    static const boost::regex rxInt("[0-9]+");
    string word = "foobar";

    if(boost::regex_match(word, rxInt)) {
        cout << "Integer" << endl;
    } else {
        cout << "Not integer" << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

$ g ++ test.cpp的错误.是否有一些理智的方式来解决这里发生的事情?

/tmp/ccRNnDit.o: In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-regex

5
推荐指数
1
解决办法
9756
查看次数

DRY CLR表值函数

我正在使用CLR表值函数来SELECT并返回使用许多变量的复杂数据库搜索的结果.

文档显示您以类似于此的方式构建此类函数:

public partial class UserDefinedFunctions
{
    private class ResultRow
    // This class holds a row which we want to return.
    {
        public SqlInt32 CustId;
        public SqlString Name;

        public ResultRow(SqlInt32 custId_, SqlString name_)
        {
            CustId = custId_;
            Name = name_;
        }
    }

    [SqlFunction(
        DataAccess = DataAccessKind.Read,
        FillRowMethodName = "Test_FillRow",
        TableDefinition = "CustId int" +
                          "Name nvarchar(50)")]
    public static IEnumerable Test()
    // This function contains the actual logic.
    {
        ArrayList results = new ArrayList();

        using (SqlConnection connection = new …
Run Code Online (Sandbox Code Playgroud)

.net c# sql-server sqlclr user-defined-functions

5
推荐指数
1
解决办法
3370
查看次数

我如何启用产品?

当我使用app_dev.php访问我的Symfony2项目时,我看到了我的网站.当我使用app.php访问它时,我看到了针对新项目的Symfony2欢迎页面.

我没有在config_dev.yml或routing_dev.yml文件中添加任何内容.我已经运行php app/console cache:clear.为什么我不能在开发模式之外看到我的项目?

这是我的routing.yml

_welcome:
    resource: "@FooBundle/Resources/config/routing.yml"
Run Code Online (Sandbox Code Playgroud)

和我的routing_dev.yml

_assetic:
    resource: .
    type:     assetic

_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_configurator:
    resource: "@SymfonyWebConfiguratorBundle/Resources/config/routing/configurator.xml"
    prefix:   /_configurator

_main:
    resource: routing.yml
Run Code Online (Sandbox Code Playgroud)

和FooBundle/Resources/config/routing.yml

homepage:
    pattern:  /
    defaults: { _controller: FooBundle:Default:index }

(other stuff...)
Run Code Online (Sandbox Code Playgroud)

symfony

4
推荐指数
1
解决办法
5046
查看次数

使用PyODBC在表中选择列名

我正在编写一个Python程序,使用PyODBC从Microsoft Access mdb文件中选择一些数据.

我需要发现几个不同表的列名.在SQL Server中,这可以通过使用类似的查询来完成

SELECT c.name FROM sys.columns c, sys.tables t
WHERE c.object_id = t.object_id
AND t.name = tableName
Run Code Online (Sandbox Code Playgroud)

但该查询在Access中不起作用.同

SELECT MSysObjects.Name FROM MSysObjects
WHERE (((MSysObjects.Flags)=0) AND ((MSysObjects.Type)=1))
ORDER BY MSysObjects.Name
Run Code Online (Sandbox Code Playgroud)

我可以获得一个非链接表名列表,但MSysObject似乎不包含列名列表.

有没有办法使用SQL来获取Access数据库中表的列名?

python ms-access ms-access-2007 pyodbc

3
推荐指数
1
解决办法
8037
查看次数

PDO 和 Microsoft SQL:必须声明表变量“@P1”

我正在尝试使用 PDO 中的绑定从 Microsoft SQL 数据库中选择一些条目。我使用的代码看起来与我在文档中找到的相似。但是,当我运行它时,我收到以下警告:

警告:PDOStatement::execute() [pdostatement.execute]:SQLSTATE[42000]:语法错误或访问冲突:1087 [Microsoft][SQL Native Client][SQL Server]必须声明表变量“@P1”。(SQLExecute[1087] at ext\pdo_odbc\odbc_stmt.c:254) in (long file path) on line 40

这是相关的代码:

$table = "[User Site]";
$user = "demo";
$sql = "SELECT * FROM ? WHERE user='?'"; 
$sth = $db->prepare($sql);
$sth->bindValue(1,  $table, PDO::PARAM_STR);
$sth->bindValue(2, $user, PDO::PARAM_STR);
$sth->execute(); //                         <-- line 40
$data = $sth->fetch(PDO::FETCH_ASSOC);
Run Code Online (Sandbox Code Playgroud)

这可能是相关的。当我尝试使用命名参数标记 (:table, :user) 而不是问号时,我得到以下信息:

警告:PDOStatement::bindValue() [pdostatement.bindvalue]:SQLSTATE[HY093]:无效的参数号:参数未在第 39 行的(长文件路径)中定义

为什么它不喜欢我准备好的声明?

php sql pdo parameterized sql-server-2008

2
推荐指数
1
解决办法
5059
查看次数

将SQL Server Express 2008降级为SQL Server Express 2005?

我使用的是Visual Studio 2008和基于服务的数据库.有一天我安装了Visual Studio C#Express 2010并自动将SQL Server升级到SQL Server 2008.我想使用Visual Studio 2008,但它不支持SQL Server 2008.请告诉我如何将SQL Server 2008降级为SQL Server 2005 ,以便它可以与Visual Studio 2008一起使用...

c# sql-server visual-studio-2010 visual-studio-2008 visual-studio

1
推荐指数
1
解决办法
648
查看次数

总和时减少收益为零

我正在计算项目在枚举中出现的次数.

irb(main):003:0> (1..3).reduce(0) {|sum, p| sum += 1 if p == 1}
=> nil
irb(main):004:0> (1..3).find_all{|p| p == 1}.length
=> 1
Run Code Online (Sandbox Code Playgroud)

reduce方法似乎应该与find_all方法具有相同的行为.为什么它会返回nil而不是1

irb(main):023:0> (1..3).reduce(0) {|sum, p| sum += 1 if p == 2}
NoMethodError: undefined method `+' for nil:NilClass
    from (irb):23:in `block in irb_binding'
    from (irb):23:in `each'
    from (irb):23:in `reduce'
    from (irb):23
    from /usr/bin/irb:12:in `<main>'
Run Code Online (Sandbox Code Playgroud)

在第一次迭代中出现了问题.可以减少不使用这种方式吗?

ruby reduce

0
推荐指数
2
解决办法
894
查看次数