Tag: ruby

Mongoid聚合$匹配Date对象不起作用?

我有一个具有这种结构的集合中的用户文档: { “_id” : ObjectId( “4fb54ef46d93b33b21003951” ), “activities” : [ { “id” : ObjectId( “4fd66f9001e7fe9f03000065” ), “type” : “checkin”, “date_time_created” : Date( 1339453328000 )}, { “date_time_created” : Date( 1337351732000 ), “date_time_updated” : Date( 1337351952635 ), “id” : ObjectId( “4fb65e346d93b3fe77000000” )} ] } 我可以根据日期轻松查询这些文档: User.where( :activities => { ‘$elemMatch’ => { :date_time_created => { ‘$gte’ => […]

无法删除闪光灯

我在迁移插件之前犯了一个错误,并写了 flash[:notice] = :label_presta_added 代替 flash[:notice] = l(:label_presta_added) 我纠正了我的错误,但似乎我的Redmine插件已经破坏了我的Redmine。 即使我删除了我的插件再次迁移,我仍然会收到此错误: Started GET “/” for 127.0.0.1 at 2016-06-01 22:21:37 +0200 Processing by WelcomeController#index as HTML Current user: admin (id=1) Rendered welcome/index.html.erb within layouts/base (28.1ms) Completed 500 Internal Server Error in 366ms (ActiveRecord: 116.0ms) ActionView::Template::Error (undefined method `html_safe’ for :label_presta_added:Symbol Did you mean? html_safe?): 97: 98: 99: […]

Rails教程(M.Hartl)第3版,第8章,如何通过关闭流氓`rails console`来解锁数据库?

我在第8.4.1章中 ,在生成迁移后我尝试运行: $ bundle exec rake db:migrate 但是给出了以下错误: rake aborted! StandardError: An error has occurred, this and all later migrations canceled: SQLite3::BusyException: database is locked: #followed by a long list of paths/filenames and line numbers… 我知道这可能是因为我没有正确退出rails console –sandbox会话而退出终端。 但是如何找到并关闭此rails console会话?

HTTParty解析Rails中的JSON

我有一个URL,显示400个YMCA位置的JSON保留信息。 我的目标是能够为每个位置提取纬度和经度,以便在地图上显示点。 我正在玩HTTParty,只是想在视图中只显示纬度和经度。 这是我的控制器: require ‘rubygems’ require ‘httparty’ class HomesController < ApplicationController include HTTParty def index uri = HTTParty.get"http://www.livestrong.org/we-can-help/ymca.json" @place = JSON.parse(uri.body) end end 以下是观点: 到目前为止,这将显示屏幕上的所有JSON。 如何挑选各个零件?

Rails 4生成无效的列名称

我有一个相当简单的查询来返回多对多关系中的第一个记录,或者如果它不存在则创建一个。 UserCategorization.where(category_id: 3, user_id: 5).first_or_create 我的模型看起来像: class UserCategorization < ActiveRecord::Base belongs_to :user belongs_to :category self.primary_key = [:user_id, :category_id] end 但是它在SQL中生成无效的列名: SQLite3::SQLException: no such column: user_categorizations.[:user_id, :category_id]: SELECT “user_categorizations”.* FROM “user_categorizations” WHERE “user_categorizations”.”category_id” = 3 AND “user_categorizations”.”user_id” = 5 ORDER BY “user_categorizations”.”[:user_id, :category_id]” ASC LIMIT 1 如果我从模型中删除self.primary_key = [:user_id, :category_id] ,它可以正确检索记录但无法保存,因为它不知道在WHERE子句中使用什么: SQLite3::SQLException: no such column: user_categorizations.: […]

在Ubuntu上运行Rails时遇到问题

首先是一些背景。 我正在尝试在全新安装的Ubuntu上运行Community Engine 。 CE是一个在Rails上运行的开放式src社交网络插件。 我能够在没有问题的情况下在我的Windows机器上运行CE并运行。 我现在决定使用Ubuntu 8.10作为我的开发环境,并坚持这个问题。 我在过去的几个晚上研究了这个,但仍然卡住了。 当我到达指令的这一步( 在此处找到 )时会发生什么: Generate the community engine migrations: $ script/generate plugin_migration 我收到以下错误: myuser@compy:~/Projects/MyProject$ script/generate plugin_migration /home/myuser/Projects/MyProject/config/../vendor/plugins/engines/boot.rb:4: This version of the engines plugin requires Rails 2.1.1 or later! (RuntimeError) from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’ from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’ from /home/myuser/Projects/MyProject/config/environment.rb:12 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’ from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’ from /home/myuser/Projects/MyProject/vendor/rails/railties/lib/commands/generate.rb:1 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in […]

如何从rails控制台将图像从url保存到本地ubuntu文件夹?

我需要从一些网站编写图像解析器,它将获取图像,其他一些信息并保存到我的本地文件夹。 所以,假设我们在此url上有图片: https : //i.stack.imgur.com/MiqEv.jpg (这是某人的SO头像) 所以我想将它保存到本地文件夹。 让我们说”~/test/image.png”我找到了这个链接 我在我的终端尝试了这个: rails console require ‘open-uri’ open(‘~/test/image.jpg’, ‘wb’) do |file| file << open('https://i.stack.imgur.com/MiqEv.jpg').read end 如您所见,我的home / test文件夹为空 我从控制台#获得此输出 我该怎么办? 我也试过这个: require ‘open-uri’ download = open(‘https://i.stack.imgur.com/MiqEv.jpg’) IO.copy_stream(download, ‘~/test/image.jpg’) 得到了这个输出: => #https://i.stack.imgur.com/MiqEv.jpg>,@ meta = {“date”=>“星期五,2016年5月6日11:58:05 GMT”,“content-type”=> “image / jpeg”,“content-length”=>“4276”,“connection”=>“keep-alive”,“set-cookie”=>“__ cfduid = d7f982c0742bf40e58d626659c65a88841462535885; expires = Sat,06-May-17 11 :58:05 GMT; path = […]

为什么控制器方法中定义的实例变量不在相应的部分中可用?

例如,给定一个基本控制器,例如: class PostsController < ApplicationController def index @post = Post.all end 实例变量@post在视图posts/index.html.erb可用,但不在部分posts/_index.html.erb 我的主要问题是:这是为什么? 如何在控制器中为部分定义方法,例如,传递实例变量? 更具体的说明:我问这个是因为我在单独的主视图welcome/index.html.erb呈现数据的方式是通过这个post部分。 我已经通过posts/_index.html.erb partial中的Post.all直接调用模型来解决这个限制,但据我所知,这忽略了MVC架构并严重限制我开发更复杂的方法,除非我真的打破约定并将它们写入视图(我认为它可能有用,但设计很差)。 目前,@ post将nil传递给部分符号:welcome / index.html.erb中的post 很明显我错过了什么,所以任何见解都会非常感激! 谢谢 相关文件: views/ posts/ _index.html.erb _new.html.erb show.html.erb welcome/ index.html.erb controllers/ posts_controller.rb welcome_controller.rb 文章/ _index.html.erb 文章/ _new.html.erb 文章/ show.html.erb welcome / index.html.erb相关部分(部分) 控制器/ posts_controller.rb class PostsController < ApplicationController def new end def create @post […]

创建与.build的关联并解决参数validation问题

使用Rails 3.2.9 我正在尝试使用.build而不是.create构建一个关联,但是获得了一个密码validation错误,我似乎无法找到解决方法。 点评如下: 我理解的方法是保存一个使用.build构建的关联的项目,你实际上必须在这个案例中对所有者进行保存。 如果你在@item上进行保存,它只是创建项而不是关联(意味着它不会保存到DB,直到current_owner.save)。 当我对所有者进行保存时,由于密码不符合validation要求,我遇到了错误。 有没有办法在我进行保存时绕过validation,因为我需要为密码实现不同的解决方案,或者只是停止抱怨并使用.create而不是.build。 下面给出的密码不符合validation错误 @item = current_owner.items.build(params[:item]) if current_owner.save Do some other work with item end 我想我可以做以下事情(出于某种原因,它似乎对我来说可能不是。想法?) @item = current_owner.items.create(params[:item]) if !@item.nil? Do some other work with item end 表设置:所有者: ID 名称 encrypted_pa​​ssword 盐 项目: ID 名称 items_owners: owner_id ITEM_ID 楷模: class Item :items_owner end class Owner :items_owner before_save :encrypt_password […]

我如何在Ruby on Rails中检查今晚9点到9点(明天)之间的当前时间

我需要检查当前时间是否在指定的时间间隔(今晚9点到明天上午9点)之间。 如何在Ruby on Rails中完成。 提前致谢