首页 \ 问答 \ Ruby PG gem连接问题(Ruby PG gem connection issue)

Ruby PG gem连接问题(Ruby PG gem connection issue)

我已经搜索了几个相关的帖子,但我找不到答案。 我是大多数人使用Mac的编码程序的学生,但是我使用的是Windows(7,Pro,64) - 因为我对这里发布的工具/软件有点紧张。

我正在尝试通过Ruby打开与pg gem的连接,我正在使用Sinatra和PostgreSQL。 我已经为PostgreSQL建立了服务器,数据库和配置路径变量,并且我已经成功安装了pg gem(在其他一些帖子中没有问题):

gem install pg -- --with-pg-config=C:\Program Files\PostgreSQL\9.5\bin

所以问题是,当我启动Sinatra并转到localhost时,我得到一个NoMethodError,未定义的方法为nil:NilClass的方法,否则适用于Mac用户。

方法是:

configure :development do
  set :db_config, { dbname: "news_aggregator_development" }
end

configure :test do
  set :db_config, { dbname: "news_aggregator_test" }
end

def db_connection
  begin
    connection = PG.connect(Sinatra::Application.db_config)
    yield(connection)
  ensure
    connection.close
  end
end

get '/articles' do
  @results = db_connection do |conn|
    conn.exec("SELECT * FROM articles")
  end
  erb :index
end

connection返回nil ,因此close方法返回未定义的方法错误。 我不认为存在语法错误,因为我已经与其他人核实了这一点,我认为它可能与pg的连接错误有关。

第一次发布所以请轻松告诉我=)如果我遗漏了任何所需信息,请致歉 - 让我知道更多上下文可能会有所帮助,我会尝试提供它! 谢谢!


I've searched several related posts for the issue I'm having but wasn't able to find an answer. I'm a student in a coding program where most people use Mac, but I'm on Windows ( 7, Pro, 64 ) - because of that I'm a bit locked in to the tools/software I'll post here.

I'm trying to open a connection through Ruby with the pg gem, and I'm using Sinatra and PostgreSQL. I've established the server, database, and configuration path variables for PostgreSQL, and I've successfully installed pg gem (didn't have an issue there as in some of the other posts) with the line:

gem install pg -- --with-pg-config=C:\Program Files\PostgreSQL\9.5\bin

So the problem is that when I boot up Sinatra and go to the localhost, I get a NoMethodError, Undefined Method for nil:NilClass on a method that otherwise works for Mac users.

The method is:

configure :development do
  set :db_config, { dbname: "news_aggregator_development" }
end

configure :test do
  set :db_config, { dbname: "news_aggregator_test" }
end

def db_connection
  begin
    connection = PG.connect(Sinatra::Application.db_config)
    yield(connection)
  ensure
    connection.close
  end
end

get '/articles' do
  @results = db_connection do |conn|
    conn.exec("SELECT * FROM articles")
  end
  erb :index
end

connection returns nil, and so the close method returns with an undefined method error. I don't think there is a syntax error as I've checked with others regarding this, and I'm thinking it might be related to a connection error with pg.

First time post so please go easy on me =) Apologies if I've left out any needed information - let me know what more context could be helpful and I will try to provide it! Thank you!


原文:https://stackoverflow.com/questions/39309670
更新时间:2023-10-25 13:10

最满意答案

我认为这会对你有用。 让我知道。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">

<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" />

  <!-- Get the parent id of every member. -->
  <xsl:variable name="parents">
    <xsl:apply-templates select="//Members//Member" mode="parents"/>
  </xsl:variable>

  <xsl:variable name="parentList" select="msxml:node-set($parents)"/>

  <xsl:template match="/">
    <xsl:element name="output">
      <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
  </xsl:template>

  <!-- Process each pattern. -->
  <xsl:template match="Pattern">
    <!-- Get the parent ids of the Members in this pattern.  -->
    <xsl:variable name="patternParentIDs">
      <xsl:copy-of select="$parentList/Member[id = current()/Member/@id]/parentid"/>
    </xsl:variable> 

    <xsl:variable name="patternParentIDList" select="msxml:node-set($patternParentIDs)"/>

    <!-- Get a distinct list of patternParentIDs. -->
    <xsl:variable name="distinctParents">
      <xsl:copy-of select="$patternParentIDList/parentid[not(. = preceding-sibling::parentid)]"/>
    </xsl:variable>

    <xsl:variable name="distinctParentList" select="msxml:node-set($distinctParents)"/>

    <!-- Determine of each parent id has all members included. -->
    <xsl:variable name="allMembers">
      <xsl:for-each select="$distinctParentList/parentid">
        <xsl:variable name="parentid" select="."/>

        <xsl:if test="count($parentList/Member[parentid=$parentid]) != count($patternParentIDList/parentid[.=$parentid])">
          <xsl:value-of select="'false'"/>
        </xsl:if>

      </xsl:for-each>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="string-length($allMembers)=0">
        <xsl:copy>
          <xsl:for-each select="$distinctParentList/parentid">
            <xsl:element name="Member">
              <xsl:attribute name="id">
                <xsl:value-of select="."/>
              </xsl:attribute>
            </xsl:element>
          </xsl:for-each>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <!-- Suppress the Members. -->
  <xsl:template match="Members"/>

  <!-- parents mode.  -->
  <xsl:template match="Member" mode="parents">
    <Member>
      <id>
        <xsl:value-of select="@id"/>
      </id>
      <parentid>
        <xsl:value-of select="../@id"/>
      </parentid>
    </Member>
  </xsl:template>

  <!-- Identity. -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>  

</xsl:stylesheet>

I think this will work for you. Let me know.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">

<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" />

  <!-- Get the parent id of every member. -->
  <xsl:variable name="parents">
    <xsl:apply-templates select="//Members//Member" mode="parents"/>
  </xsl:variable>

  <xsl:variable name="parentList" select="msxml:node-set($parents)"/>

  <xsl:template match="/">
    <xsl:element name="output">
      <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
  </xsl:template>

  <!-- Process each pattern. -->
  <xsl:template match="Pattern">
    <!-- Get the parent ids of the Members in this pattern.  -->
    <xsl:variable name="patternParentIDs">
      <xsl:copy-of select="$parentList/Member[id = current()/Member/@id]/parentid"/>
    </xsl:variable> 

    <xsl:variable name="patternParentIDList" select="msxml:node-set($patternParentIDs)"/>

    <!-- Get a distinct list of patternParentIDs. -->
    <xsl:variable name="distinctParents">
      <xsl:copy-of select="$patternParentIDList/parentid[not(. = preceding-sibling::parentid)]"/>
    </xsl:variable>

    <xsl:variable name="distinctParentList" select="msxml:node-set($distinctParents)"/>

    <!-- Determine of each parent id has all members included. -->
    <xsl:variable name="allMembers">
      <xsl:for-each select="$distinctParentList/parentid">
        <xsl:variable name="parentid" select="."/>

        <xsl:if test="count($parentList/Member[parentid=$parentid]) != count($patternParentIDList/parentid[.=$parentid])">
          <xsl:value-of select="'false'"/>
        </xsl:if>

      </xsl:for-each>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="string-length($allMembers)=0">
        <xsl:copy>
          <xsl:for-each select="$distinctParentList/parentid">
            <xsl:element name="Member">
              <xsl:attribute name="id">
                <xsl:value-of select="."/>
              </xsl:attribute>
            </xsl:element>
          </xsl:for-each>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <!-- Suppress the Members. -->
  <xsl:template match="Members"/>

  <!-- parents mode.  -->
  <xsl:template match="Member" mode="parents">
    <Member>
      <id>
        <xsl:value-of select="@id"/>
      </id>
      <parentid>
        <xsl:value-of select="../@id"/>
      </parentid>
    </Member>
  </xsl:template>

  <!-- Identity. -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>  

</xsl:stylesheet>

相关问答

更多
  • 正如评论中所提到的,这里使用的技术是Muenchian Grouping 。 起初它看起来很难,但它是一种非常有用的技术。 在您的情况下,您希望由他们的作者查找“选定”书籍,因此您将定义一个键,如下所示: 然后,为了获得不同的作者,您可以查看所有element元素,并选择在给定作者的键中首先出现的 ...
  • XSLT 1.0没有日期概念(甚至XSLT 2.0也只识别ISO-8601格式的日期)。 所以你需要分两步完成: 将日期转换为YYYMMDDhhmmss形式的可排序字符串。 由于必须将12小时时间转换为24小时格式,这一点变得更加复杂。 对结果节点进行排序,并输出其中一个节点(或最后一个,具体取决于排序顺序)。 XSLT 1.0
  • 情况1 无论您使用的是XSLT 1.0还是2.0,架构感知或非架构感知,当且仅当属性不存在时,not(@dothis)才返回true。 (在2.0术语中,not()使用其参数的有效布尔值,如果属性存在则@dothis的有效布尔值为true,如果不存在则为false。) 相反,@ dothis = true()的值取决于具体情况。 在XPath 1.0中,您将节点集与布尔值进行比较,并通过将节点集转换为布尔值来完成 ...
  • 问题是preheader和logo元素重复两次。 你有这些重复行的原因是这条指令: 这有效地将模板应用于所有 surface后代 - 包括您已经处理过的preheader和brand节点。 避免这种情况的一种方法是仅将模板选择性地应用于此时所需的节点:
    我认为这会对你有用。 让我知道。
  • 好的,这个模板应该符合您的需求: ...
  • 我担心这可能比它看起来要复杂得多。 如果要为每个文件夹显示节点(无论是否包含文档),必须首先将位置标记为单个文件夹。 接下来,您必须从结果中删除重复项。* 第三步是将文件夹排列为分层结构,并将文档重新附加到其文件夹中。 这是一个草图,您可以将其用作样式表的基础: XSLT 1.0
  • 第一个表达式匹配任何元素父级。 第二个表达式仅在父item是item元素时匹配。 这是唯一的区别。 我无法想象任何重大的性能影响,因为两个节点测试都可以在恒定的时间内执行。 请注意XPath 1.0规范中的这一行: 除根节点之外的每个节点都只有一个父节点,它是元素节点或根节点。 在实践中,这意味着parent::*匹配除根元素之外的任何父级。 要演示,请考虑以下简单示例文档: 然 ...
  • XSLT没有精确定义排序应该如何运作; 结果是实现定义的。 在Saxon的最新版本中,有一个排序可以做你想要的,但是假定XSLT 2.0; 事实上,它假设撒克逊人。 在XSLT 1.0中以可移植的方式执行它并不容易,尤其是因为您无法调用递归模板来计算排序键。 XSLT doesn't define precisely how sorting should operate; the results are implementation-defined. In recent releases of Saxon ...
  • Muenchian分组的例子 ...... XML输入 (修改为格式良好并添加child值的示例)

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)