Tag: ruby on rails 4

找不到rvm命令

我在RVM的官方网站上安装了rvm with rails,我指定了教程建议的命令。 安装时我的系统有rails 4.0.0,rvm 01/23/12,ruby 2.0.0,bundler gem 1.3.5和2.0.3但是我需要安装ruby 1.9来练习我正在做的课程。 “Zombies 2的Rails”我尝试执行指令rvm install 1.9.3并且控制台返回此消息: rvm: command not found

从Rails 4应用程序调用大量SQL

我有一个Rails 4应用程序,我与sidekiq一起使用来运行异步作业。 我通常在我的Rails应用程序之外运行的一个作业是一大堆复杂的SQL查询,它们无法真正由ActiveRecord建模。 这组SQL查询与我的Rails应用程序的连接是,它应该在我的一个控制器操作被调用时执行。 理想情况下,我会从控制器中的Rails应用程序中为Sidekiq排队,继续运行查询。 现在它们存储在外部文件中,我不完全确定Rails运行所述SQL的最佳方法是什么。 任何解决方案都很受欢

Rails控制台无法在服务器上运行

当我在Capistrano部署的Current文件夹中的服务器上通过SSH运行bundle exec rails console production或rails console production ,我得到: Usage: rails new APP_PATH [options] Options: (…) 解释开始一个新的应用程序。 在当地它的工作原理。 为什么我不能远程启动控制台?

在Rails 4.1中使用Searchkick gem在Elasticsearch中设置Facets

我希望用户能够轻松找到一个系列,所以想要设置方面。 我已按照seachkick的指示进行操作 ,一切正常,但是当我设置Facets时,我得到以下作为返回。 我希望它像在他们的文档中一样。 希望有人能提供帮助。 我在myapp.com/movies中得到了这个 { “name”=> { “_type”=> “terms”, “missing”=> 0, “total”=> 1, “other”=> 0, “terms”=> [ { “term”=> “Bloop”, “count”=> 1 } ] }, “imdb”=> { “_type”=> “terms”, “missing”=> 0, “total”=> 1, “other”=> 0, “terms”=> [ { “term” => “http://www.bloop.com”, “count” => 1 } ] } } #app/views/movies/index.html.erb #app/controllers/movies_controller.rb def index query […]

Heroku上的Rails 4.0.1,无法创建数据库

我无法得到rake db:migrate在Heroku上运行我的rails 4.0.1应用程序。 我猜我没有正确配置postgres …但是阅读heroku上的文档并没有真正帮助,我不知道该怎么做。 我对heroku或postgres不太了解。 任何帮助或资源将不胜感激。 如果我还有其他任何内容可以发帖,请告诉我。 (另外,我正在使用设计,如果这很重要) 当我运行heroku run rake db:migrate我得到这个: Running `rake db:migrate` attached to terminal… up, run.5077 PG::UndefinedTable: ERROR: relation “users” does not exist LINE 5: WHERE a.attrelid = ‘”users”‘::regclass ^ : SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = […]

如何在Rails 4 App中启用CORS

我正要把我的头发拉出来…我从早上开始尝试在这个Rails应用程序中启用CORS,它只是不起作用。 我试过这个 ,使用Rack Cors Gem , 这个答案和这篇文章都没有成功。 有人能指出我正确的方向吗? 这是我的js: var req = new XMLHttpRequest(); if (‘withCredentials’ in req) { // req.open(‘GET’, “https://api.github.com/users/mralexgray/repos”, true); req.open(‘GET’, “http://www.postcoder.lc/postcodes/” + value, true); // Just like regular ol’ XHR req.onreadystatechange = function() { if (req.readyState === 4) { if (req.status >= 200 && req.status < 400) { // JSON.parse(req.responseText) etc. […]

Rails:从外部控制器渲染视图

我正在尝试使用视图创建HTML字符串。 我想从一个不是控制器的类中渲染它。 如何在控制器外部使用rails渲染引擎? 与ActionMailer的做法类似? 谢谢!

如何在Rails 4中清理原始SQL

在Rails 3中,我可以使用sanitize_sql_array来清理那些需要原始SQL查询的偶然时刻的原始SQL。 但是这似乎已经在Rails 4中删除了,或者没有那么多删除,而是转移到ActiveRecord :: Sanitization。 但是,我现在无法弄清楚如何调用sanitize_sql_array ,那么在Rails 4中清理原始SQL的最佳方法是什么? 我想澄清一下,我在这里讨论完整的原始SQL查询,而不是使用Rail的模型。 我知道这不是最佳实践,这正是我必须为这个特定查询做的事情,因为它不能用Rails的好的ActiveRecord接口来表示(相信我,我已经尝试过了)。 这是一个示例调用,它明显比我的查询实际上看起来更简单: query = “SELECT * FROM users LEFT OUTER JOIN posts ON users.id=posts.user_id AND posts.topic_id = ‘#{topic.id}'” # ^- Obviously bad and very vulnerable, this is what we’re trying to fix ActiveRecord::Base.connection.select_all(query)

Paperclip错误:’avatar_file_name’需要attr_accessor所需的模型

然后,我想使用Paperclip为每个清单提供照片。 我将相应的代码添加到列表show.html.erb,listing.rb模型,listings_controller.rb和_form.html.erb部分。 当我尝试上传列表的图像时,我收到此错误: Paperclip::Error in ListingsController#update Listing model missing required attr_accessor for ‘avatar_file_name’ listing_controller的第44行: def update respond_to do |format| if @listing.update(listing_params) format.html { redirect_to @listing, notice: ‘Listing was successfully updated.’ } format.json { head :no_content } else 要尝试的一些事情:即向listing.rb模型添加一些代码,以使:avatar的可接受图像更加健壮。 以下是几个stackoverflowpost中提到的添加到listing.rb模型的内容: validates_attachment_content_type :avatar, :content_type => %w(image/jpeg image/jpg image/png) 不幸的是,当我附加图像时,我仍然会收到相同的错误。 当我没有附加图像时,我的默认图像被正确加载并正确创建列表。 我的清单模型: class Listing { :medium => “150x”, […]

Rails4:多个控制器共享的方法

我有两个控制器,即1)carts_controller 2)orders_controller class CartsController < ApplicationController helper_method :method3 def method1 end def method2 end def method3 # using method1 and method2 end end 注意: method3使用method1和method2 。 CartsController有showcart.html.erb视图,它使用method3并且工作正常。 现在按顺序查看,我需要显示购物车( showcart.html.erb ),但是因为帮助方法method3在carts_controller定义所以它无法访问它。 怎么解决?