草庐IT

with-pg-config

全部标签

ruby-on-rails - rails : Using form fields that are unassociated with a model in validations

在RubyonRails应用程序中,我尝试使用来自与验证模型无关的字段的信息。这里以模型的一部分为例(整个模型有点大):classScorecard那么如何从模型中访问params呢? 最佳答案 不要让参数偷偷靠近模型。在这种情况下没有Controller的意义。相反,从Railscasts查看这一集它讨论了不进入数据库但仍可用于验证的虚拟属性。虚拟属性不需要相应的模型属性。定义类的局部属性,例如保存状态的@no_fairways。classScoreCard现在在你的表单中,你可以写:

ruby-on-rails - 在 respond_with 散列中包含一个虚拟属性

我试图在respond_toJSON散列中包含一个虚拟属性/方法。模型(employee.rb)attr_reader:my_methoddefmy_methodreturn"foobar"endController(employees_controller.rb)respond_to:jsondefindex@employees=Employee.allrespond_with(:data=>@employees,:total=>Employee.all.count)end重要的是,我将“data”作为“employees”集合的json根,并将“total”包含在散列中。这很好用,

ruby - Rails3 : combine scope with OR

我需要将名称范围与or运算符组合...像这样的东西:classProduct谢谢 最佳答案 来自AreldocumentationTheORoperatorisnotyetsupported.Itwillworklikethis:users.where(users[:name].eq('bob').or(users[:age].lt(25)))ThisRailsCast向您展示如何使用.or运算符。但是,当您拥有ActiveRecord::Relation实例时,它可以与Arel对象一起使用。您可以使用Product.name_a.

ruby - `sort_by' : comparison of Array with Array failed (no nil data)

classCustomSorterattr_accessor:start_date,:availabledefinitialize(start_date,available)@start_date=Time.mktime(*start_date.split('-'))@available=availableendendcs1=CustomSorter.new('2015-08-01',2)cs2=CustomSorter.new('2015-08-02',1)cs3=CustomSorter.new('2016-01-01',1)cs4=CustomSorter.new('2015-0

ruby-on-rails -/config/initializers/secret_token.rb 未生成。为什么不?

目前正在阅读Rails教程,我需要对/config/initializers/secret_token.rb进行一些修改,但是,我无法在initializers目录。我正在运行最新版本的Rails。这是我在终端中用来创建rails项目的行:railsnewsample_app有人知道为什么它没有出现吗? 最佳答案 感谢您指出这一点。问题可能是由于使用Rails4.1而不是Rails教程中指定的Rails4.0。就是因为这样的问题Section1.2.2州(原文为粗体)Unlessotherwisenoted,youshoulduse

ruby-on-rails - 为什么 PG::UniqueViolation: ERROR: duplicate key value violates unique constraint?

我有一个模型Post,每次创建帖子时,我都希望同时创建一个新的Moderation实例。所以在post.rb中我使用回调after_save:create_moderation然后写一个私有(private)方法:...includeReportableafter_save:create_moderationprivatedefcreate_moderationself.create_moderation!(blog:Blog.first)end但是在创建提案时出现此错误:PG::UniqueViolation:ERROR:duplicatekeyvalueviolatesunique

ruby-on-rails - Ruby on Rails config.secret_token 错误

我刚开始学习RubyonRails。我遵循了很多安装示例,但是当我运行示例时出现此错误为cookiesession数据生成完整性散列需要一个secret。在config/initializers/secret_token.rb中使用config.secret_token="somesecretphraseofatleast30characters"我搜索它,但没有看到太多帮助。请帮忙。平台:MacOSX。 最佳答案 生成新secrettoken的最简单方法是运行rakesecret在命令行。

ruby-on-rails - 如何修复 PG::DuplicatePstatement: ERROR?

我想知道如何防止此类错误。到目前为止,我尝试通过AWSOpsworks的数据库配置禁用准备好的语句:例如:数据库配置},"deploy":{"app_name":{"database":{"adapter":"postgresql","prepared_statements":false,"username":"username","database":"db_name_production","host":"cool_host.com","password":"easy"},错误日志PG::DuplicatePstatement:ERROR:preparedstatement"a6"

ruby-on-rails - 缺少 `secret_token` 和 `secret_key_base` - Rails 4.2.0 with RVM

最近,我从Git中提取了一个存储库。启动服务器后,我收到以下消息:缺少secret_token和secret_key_base。这可能是因为我在我的.gitignore中包含了secrets.yml。我目前的设置Ubuntu14.04ruby2.2.0p0rails4.2.0rvm1.26.11本地服务器(非远程)开发环境许多在线资源指出我必须使用rakesecret生成新key并将其添加到secrets.yml中。将key放在secrets.yml中并重新启动Rails服务器不起作用。Edited:Addedcontentsofsecrets.ymlbelow.-04/30/159:

ruby-on-rails - Rails 嵌套 with_option :if used in validation

validate:updatable?#Firstvalidationthereiswith_options:if=>Proc.new{|object|object.errors.empty?}do|updatable|updatable.with_options:if=>"self.current_step==basic"do|step|validates....bla-blabla因此,在进行任何验证之前,updatable子例程被调用,它用适当的错误填充errors[:base]数组,这意味着该对象不可更新.如果在此子例程中发现任何错误,我希望它跳过其余的验证,但上述示例不工作-