Tag: ruby

params.require()。permit不能按预期工作

我有这个控制器 class PeopleController < ApplicationController def new @person = Person.new @person.phones.new end # this is the action that gets called by the form def create render text: person_params.inspect # @person = Person.new(person_params) # @person.save # redirect_to people_path end def index @person = Person.all end private def person_params params.require(:person).permit(:name, phones_attributes: [ :id, :phone_number ]) end end […]

将rails mongodb应用程序部署到heroku时“预编译资产失败”

我正在尝试使用mongodb部署rails应用程序到heroku,但不断收到错误: NoMethodError: undefined method `active_record’ for # … Precompiling assets failed. 在问之前我已经尝试过所有内容,例如以下解决方案 – ( Heroku预编译资产失败 , Ruby on Rails预编译资产失败 )但我仍然无法使其工作 1.当我创建应用程序时,我使用-O选项跳过活动记录: rails new app -T -O 2.我删除了sqlite3。 这是Gemfile: source ‘https://rubygems.org’ gem ‘rails’, ‘4.1.8’ gem ‘sass-rails’, ‘~> 4.0.3’ gem ‘uglifier’, ‘>= 1.3.0’ gem ‘coffee-rails’, ‘~> 4.0.0’ gem ‘jquery-rails’ gem ‘turbolinks’ gem ‘jbuilder’, ‘~> 2.0’ gem ‘sdoc’, […]

模型中的酉常数

我有一个使用Mongoid的非常简单的模型。 我已经添加了使用Redcarpet解析MD并存储它。 但是在update_attributes期间,它会抛出exception。 运行模型并通过rails c运行更新工作正常。 class Post include Mongoid::Document field :contents_markdown field :contents key :title before_create :markdown before_save :markdown protected def markdown if self.contents_markdown self.contents = Redcarpet.new(self.contents_markdown).to_html.html_safe end end end 这是控制器爆炸了。 def update @post = Post.find(params[:id]) respond_to do |format| if @post.update_attributes(params[:post]) format.html { redirect_to @post, notice: ‘Post was successfully updated.’ } format.json { head :ok […]

如何使用activerecord的查询界面和范围来识别比赛中的最爱

我有两个模型Starter和Race。 Starter belongs_to :race Race has_many :starters Starter和race的属性如下: Starter attributes: id, race_id, finish_position, odds Race: id, race_date, race_number, field_size 我正在努力完成两件事: 选择每场比赛中的最爱 。 #对于特定比赛,赔率最低的初学者 * 选择任何打败的collections夹 *#一个有finish_position> 1 *的collections夹 确定collections和打败collections的逻辑非常简单(见上文),但我很难将逻辑转换为activerecord,这也是一个例子,可以使用activerecord范围。 这是我最喜欢的尝试: Starter.joins(:race).where(:finish_position => minimum(finish_position)) 这不起作用,但我仍在努力。 我想理想的是拥有一个最喜欢的范围和一个打败的范围。

在rails中设置测试环境变量而不放入源代码

我正在使用Twilio作为应用程序并在生产中使用heroku的CLI设置auth令牌。 我正在使用sms-spec ( https://github.com/monfresh/sms-spec )在本地测试我的应用程序的Twilio集成。 我想在测试环境中将ENV[‘TWILIO_AUTH_TOKEN’]为我的令牌。 每当我进行更改时,我都会使用guard来自动运行测试,因此我不希望每次运行测试时都必须手动设置ENV变量。 出于安全原因,我也不想将令牌放在源代码中。 有没有办法可以为我的本地测试环境设置ENV变量,使其成为永久性而不是我的源代码? 我花了几个小时研究这个,似乎无法找到如何做到这一点的好解释。 任何帮助深表感谢 :)

如何在我的rails应用程序中编辑production.rb以使用heroku和paperclip使其正常工作?

我在rails应用程序中使用paperclip + AWS来上传图像。 当我在视图中引用图像时 当前图片url显示为: http://s3.amazonaws.com/rockywolfugc/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682 我需要他们出现: http://rockywolfugc.s3-us-west-2.amazonaws.com/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682 在http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3我看到有一个选项“:s3_domain_url”,但我不太确定如何使用它。 以下是production.rb的相关部分 config.paperclip_defaults = { :storage => :s3, :s3_credentials => { :bucket => ENV[‘S3_BUCKET_NAME’], :access_key_id => ENV[‘AWS_ACCESS_KEY_ID’], :secret_access_key => ENV[‘AWS_SECRET_ACCESS_KEY’] } } 我在哪里将s3_domain_url集成到此文件中? 另外,我需要在heroku上做些什么才能让它运行? 示例:heroku config:set xxxx = yyyy

Rails ArgumentError(缺少必需:bucket选项)修复不起作用?

我的Rails应用程序托管在Heroku上,但我需要让Paperclip在本地工作,以便我可以在测试中上传照片。 我真的很接近,但我坚持“缺少必需:桶选项”错误。 我尝试在环境文件中移动设置,以及添加带有AWS凭据的config / application.yml文件,但没有骰子。 这是我在development.rb中的配置(在production.rb中看起来相同): config.paperclip_defaults = { :storage => :s3, :s3_credentials => { :bucket => ENV[‘S3_BUCKET_NAME’], :access_key_id => ENV[‘AWS_ACCESS_KEY_ID’], :secret_access_key => ENV[‘AWS_SECRET_ACCESS_KEY’] } } 我试过移动:bucket属性在s3_credentials之外,但它没有改变任何东西。 Application.yml看起来像这样: AWS_ACCESS_KEY_ID=”xxxxxxxxxxxxxxxxx” AWS_SECRET_ACCESS_KEY=”xxxxxxxxxxxxxxxxxxxx” S3_BUCKET_NAME=”xxxxxx” 我还安装了dot-env gem并将s3.env文件添加到我的应用程序的索引中,该索引包含与上面相同的凭据,但即使这样也没有用。 也没有将我的Paperclip配置添加到我的模型中的has_attached_file中。 我该怎么办??

ActiveRecord对子集或记录的计算

我有一个带有points属性的Game模型,我想计算前20个得分的总和。 我可以采取积分并让ruby计算总和,如: Game.order(‘points desc’).limit(20).pluck(:points).sum 但我很好奇是否有一种直接的方法让AR产生一个SQL聚合计算来实现同样的目的。 以下天真的尝试不起作用: Game.sum(:points, order: ‘points desc’, limit: 20) SELECT SUM(`games`.`points`) FROM `games` 谢谢

有没有更好的方法来编写这个named_scope?

我正在使用此named_scope来搜索具有与用户输入的任何单词匹配的描述的产品。 例如, Product.description_like_any(“choc pret”) 将返回名称为的产品 “巧克力吧” “巧克力覆盖椒盐卷饼” “迷你巧克力小马” 这是我写的named_scope(有效) named_scope :description_like_any, (lambda do |query| return {} unless query conditions = [] values = [] for q in query.split(/\s+/) conditions << "(`products`.description LIKE ?)" values < [conditions.join(‘ AND ‘), *values] } end) 有没有更好的方法来写这个? 也许我错过了一两个Rubyism / Railism? 解 将scope_procedure与Searchlogic结合使用,可以更简单的方式完成。 注意,该解决方案甚至可以利用Searchlogic的_or_语法将两个范围连接在一起。 :keywords scope_procedure查找与product.description或product.vendor.name匹配的product.vendor.name ; 全部有一个文本字段! 模型 # app/models/product.rb […]

Rails Geokit太多查询错误

我试图做一个小应用来测试gem“geokit”,“〜> 1.8.4” 它在开始时工作,但随后开始引发此错误 Geokit ::地址解析器:: TooManyQueriesError 最多我用这行代码做了100次查询 suggested_bounds = Geokit :: Geocoders :: GoogleGeocoder.geocode(params [:location]) 谁能帮我 谢谢