Tag: ruby on rails 4.2

Rails 4.2.0中的简单整数赋值的RangeError应该通过validation捕获

*更新:现在已修复4.2.stable和4.2.1 * 在Rails 4.2.0(和当前的4.2.stable)中, ensure_in_range方法在ARvalidation之前发生,产生RangeError 如果我做一些简单的事情 @obj.threshold = 10_000_000_000 在具有postgres类型整数的列上 threshold | integer | 它产量 RangeError:10000000000超出ActiveRecord :: ConnectionAdapters :: PostgreSQL :: OID :: Integer的范围,限制为4来自… / 2.0.0-p598 / lib / ruby​​ / gems / 2.0.0 / bundler / gems / rails -62e9e61f2d1b / activerecord / lib / active_record / type / integer.rb:41:在`ensure_in_range’中 这是真的! 但告诉用户。 有一个像ActiveRecord模型validation validates […]

在模型之间共享枚举声明值

我在以下属性上应用枚举 : transparency 相同的属性(使用枚举)用于两个不同的模型: Category和Post 是否可以在模型之间共享枚举值,以避免代码重复: enum transparency: %w(anonymous private public)

资产管道在我的Rails 4.2应用程序中的’application.scss’中不起作用

我是Rails的新手并且如果这是一个愚蠢的问题而道歉。 但我无法解决我的问题:我添加了Bootstrap到我的新应用程序,但它仍然没有使用任何新的样式。 我已将application.css重命名为application.scss并创建了这样的结构: |-stylesheets |-elements |–articles |—article.scss |-application.scss Application.scss /* User styles * Plugins first */ @import “bootstrap-sprockets”; @import “bootstrap”; @import “elements/**/*”; /* * This is a manifest file that’ll be compiled into application.css, which will include all the files * listed below. * * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, […]

在Rails 4.2中使用redirect_to时,为什么错误的参数数量错误?

在应用程序的Rails 4.1.1版本中,我在articles_controller中有以下创建方法: def create @article = Article.new(article_params) authorize @article if @article.save flash[:notice] = “Successfully created article.” redirect_to edit_article_path(@article) else render :new end end 但是,在更新到Rails 4.2后,尝试重定向时会出现以下错误: wrong number of arguments (2 for 1) 为什么会出现此错误以及如何解决?

如何使Rails 4.2与Postgres Jsonb一起使用?

我看过一些博客 post声称rails 4.2在Postgres 4.2中添加了对新Jsonb数据类型的支持。 但是,谷歌搜索让我得到如何实际使用数据类型的零结果。 因为我不依赖于键顺序而且我希望我的应用程序快速,所以我非常希望在我的一个模型中使用Jsonb而不是json。 它实际上是在4.2中添加的,如果是这样,你如何使用它?

Rails 4:收到以下错误:表单中的第一个参数不能包含nil或为空

我在我的项目中收到以下错误:表单中的第一个参数不能包含nil或为空。 我正在尝试为我的代码创建一个编辑页面。 Rails相当新,并试图学习没有脚手架。 控制器: class BooksController < ApplicationController def new @book = Book.new @authors = Author.all end def edit @book = Book.find(params[:id]) end def show #Notice how the @books is plural here. @books = Book.all @authors = Author.all #@books = Book.where(id: params[:id]) end #Create method will save new entries def create @book = Book.new(book_params) @authors […]

NoMethodError – 升级Rails 4.2后的Paper Trail问题的未定义方法`timestamp_sort_order’

当我将rails 3.2迁移到rails 4.2时,我使用paper_trail来跟踪跨国变化,有以下问题: NoMethodError – 未定义的方法`timestamp_sort_order’

如何为变量标记的当前输出分配变量

我在使用select标签时遇到了一些麻烦。 我想根据select标签上显示的名称为变量赋值: “form-control”}) %> 值显示正确,但我想将当前选定的值分配给变量,以便稍后可以在同一页面上使用它来显示具有所选产品相关详细信息的内容。 例如: @product = currently_displayed_name 这是可能的Rails和如何做到这一点? 或者我可能需要使用其他资源吗?

如何在启动Rails控制台时自动运行代码?

假设每次Rails控制台出现时我都想要问候: Scotts-MBP-4:ucode scott$ rails c Loading development environment (Rails 4.2.1) Hello there! I’m a custom greeting 2.1.5 :001 > 在哪里我会把puts ‘Hello there! I\’ma custom greeting’ puts ‘Hello there! I\’ma custom greeting’吗? 另一个Stackoverflow回答建议,我也在其他地方读过这个,我可以把它放在这样的初始化器中: # config/initializers/console_greeting.rb if defined?(Rails::Console) puts ‘Hello there! I\’ma custom greeting’ end 虽然这对我不起作用:(。即使没有if defined?(Rails::Console)我仍然没有输出。看起来像我进入控制台时初始化器没有运行,尽管其他人建议。

如何覆盖Ruby Ranges的..和…运算符以接受Float :: INFINITY?

我想覆盖Ruby的Range的..和…运算符。 原因是,我正在使用数据库中的无限日期范围。 如果你从Postgres中infinty一个infinty日期时间,你会在Ruby中得到一个Float::INFINITY 。 这个问题是,我不能使用Float::INFINITY作为范围的结尾: Date.today…Float::INFINITY => Wed, 02 Nov 2016…Infinity DateTime.now…Float::INFINITY # ArgumentError: bad value for range Time.now…Float::INFINITY # ArgumentError: bad value for range …但我在代码中经常使用..和…语法。 为了能够构造范围,您需要使用DateTime::Infinity.new : Date.today…DateTime::Infinity.new => Wed, 02 Nov 2016…# DateTime.now…DateTime::Infinity.new => Wed, 02 Nov 2016 12:57:07 +0000…# Time.now…DateTime::Infinity.new => 2016-11-02 12:57:33 +0000…# 但我每次都需要进行Float::INFINITY – > DateTime::Infinity.new转换: model.start_time…convert_infinity(model.end_time) 有没有办法可以覆盖..和…运算符,以便我可以合并转换函数并保持语法糖?