Tag: ruby on rails 4

更改post循环排序的链接

我正试图在我的页面中实现链接,允许我更改我的post显示的顺序。类似于Reddit上的’Recent’,’Hot’和’Oldest’。 我目前默认拥有 PostsController.rb def index @posts = Post.order(‘created_at DESC’).all.paginate(page: params[:page], per_page: 20) end 我如何添加链接到一个方法来反转post到ASC的流量,或者显示像模型上的特定列下降的post,如视图? 乔恩

在Action Mailer中设置实例变量?

我想知道在Mailers中设置实例变量的干净和传统方法是什么? 目前,我已经在Mailer重新定义了initialize方法,并且随后在从Mailerinheritance的任何邮件程序中需要时覆盖某些实例变量。 class Mailer < ActionMailer::Base attr_reader :ivar def initialize super @ivar = :blah … end end 这对我来说似乎很奇怪,因为new是邮件的私有方法。 例如,如果我尝试在rails控制台中检索这些,我需要执行以下操作: mailer = Mailer.send(:new) mailer.ivar 我也考虑过将它们添加到default哈希中,如下所示: class Mailer < ActionMailer::Base default ivar: :blah, … end 唯一的问题是我需要创建一个这样的方法来检索ivars: def default_getter(ivar) self.class.default[ivar] end 这两种方式对我来说都不是特别干净。 我考虑过使用类变量,但我想知道是否有人可以提出更清洁的方法。 谢谢。

Rails通过有条件地使用多个id查询has_many:

我正在尝试通过LocationFeature模型为具有位置和function的网站构建过滤系统。 基本上它应该做的是基于特征ID的组合给我所有位置。 例如,如果我调用该方法: Location.find_by_features(1,3,4) 它应该只返回具有所有选定function的位置。 因此,如果某个位置具有feature_ids [1,3,5],则不应返回,但如果它具有[1,3,4,5]则应该返回。 但是,目前它给我的地点有其中任何一个。 因此,在此示例中,它返回两者,因为每个feature_id中都存在一些feature_ids。 这是我的模特: class Location < ActiveRecord::Base has_many :location_features, dependent: :destroy has_many :features, through: :location_features def self.find_by_features(*ids) includes(:features).where(features: {id: ids}) end end class LocationFeature < ActiveRecord::Base belongs_to :location belongs_to :feature end class Feature < ActiveRecord::Base has_many :location_features, dependent: :destroy has_many :locations, through: :location_features end 显然,这段代码并没有按照我想要的方式运行,我只是无法理解它。 我也尝试过这样的事情: Location.includes(:features).where(‘features.id = […]

使用Rails中的Devise创建虚拟属性4

我的用户模型没有:name字段,但我想在我的注册表单中包含:name字段,该字段将用于在after_create创建另一个模型的after_create 。 class User < ActiveRecord::Base after_create :create_thing private def create_thing @thing = Thing.new @thing.name = @thing.save! end end 如何从注册表单中获取名称?

为什么rails生成空模型?

我正在尝试生成一些模型,但它们是在没有属性的情况下生成的。 我使用的是linux系统,rails版本是:rails –version Rails 4.0.0 我尝试使用以下命令生成模型: rails g scaffold Bsdsd description:string test:string oaso:integer 和 rails g model Asdsd description:string test:string oaso:integer 第一个导致这个空类模型的其他一切正常: class Bsdsd < ActiveRecord::Base end 第二个结果是测试文件,迁移文件(包含属性)和此类模型: class Asdsd < ActiveRecord::Base end 我该如何纠正这种行为?

Heroku:无法通过Bundler安装gem

我尝试使用git push heroku master -f命令将我的Rails应用程序推送到Heroku,我得到了这个: Total 0 (delta 0), reused 0 (delta 0) remote: Compressing source files… done. remote: Building source: remote: remote: —–> Ruby app detected remote: —–> Compiling Ruby/Rails remote: —–> Using Ruby version: ruby-2.0.0 remote: —–> Installing dependencies using 1.9.7 remote: Running: bundle install –without development:test –path vendor/bundle –binstubs vendor/bundle/bin -j4 –deployment […]

我如何彻底摆脱团队城市的Rails 4.2应用程序?

我通常使用Vim进行Rails开发,我决定尝试使用RubyMine来查看它是什么样的。 这不是一半坏,但我觉得它太臃肿了。 现在,当我运行rake test它正在“通过”团队城市运行测试(我不完全理解它在做什么)。 在任何时候我都不记得安装teamcity。 我怎么能完全摆脱它呢? 在我的测试以干净利落的方式运行之前,保护和最小/自豪。 谢谢。

通过rails中的迁移添加自动增量

如何通过Rails Migration向我的表users名为user_number的列添加自动增量属性。 我已经有一个id字段,它是它的主键,它是一个自动增量字段。 我试图创建一个新的自动增量字段而不删除此id字段..

未定义的方法`full_title’

我收到以下错误: undefined method `full_title’ 在这一行: 在我的布局文件上: true %> true %> 我正在尝试使用页面标题做类似于Mike Hartle rails教程的事情,除了我没有使用测试。 所以我没有在spec文件夹中创建支持文件。 我实际上没有spec文件夹。 我相信没有这个代码的支持文件: def full_title(page_title) base_title = “Ruby on Rails Tutorial Sample App” if page_title.empty? base_title else “#{base_title} | #{page_title}” end end 导致错误。 解决这个问题的正确方法是你不想创建测试,因此不想要spec文件夹? 我在哪里可以放这个代码?

Ruby / rails:mongoid with mongo(gem); 一场激烈的冲突? 如何处理不同的版本?

如何处理gem版错配? Mongoid 4.0.0(最新版)使用moped(2.0.0),需要bson 2.3 Mongo 1.10.2(最新)使用bson 1.10.2 我不能一起使用mongoid 4.0.0和mongo 1.10.2,但我可以在过去,当mongoid使用bson 1.x. 有谁知道如何继续使用mongo和mongoid? -daniel 链接: https://rubygems.org/gems/mongoid https://rubygems.org/gems/moped https://rubygems.org/gems/mongo