Tag: ruby on rails

无法反转看似简单的rails迁移 – 获取“changed_table.column可能不是NULL”错误

我在我的开发数据库(sqlite3)中有一个表’发票’,其中填充了少量测试数据。 我想在其中添加一个列’invoice_number’并设置如下的迁移: class AddInvoiceNumberColumnToInvoices < ActiveRecord::Migration def self.up add_column :invoices, :invoice_number, :integer end def self.down remove_column :invoices, :invoice_number end end 我运行rake db:migrate ,它似乎迁移得很好。 但是,当我尝试通过ActiveRecord访问此列时,它似乎并不存在。 我决定撤消这个迁移,然后再试一次(不知道我要尝试什么,但我想我会先撤消它),使用rake db:migrate VERSION=’whatever_the_migration_before_this_one_was_called’ 。 这失败并显示错误消息 == AddInvoiceNumberColumnToInvoices: reverting =============================== — remove_column(:invoices, :invoice_number) rake aborted! An error has occurred, this and all later migrations canceled: altered_invoices.invoice_number may not be NULL 我找不到任何关于此错误的文档。 有人能够解释我做错了什么,更重要的是我如何解决这个问题?

ruby 2.1 rails 4个太阳黑子solr测试在套件中失败但是单独通过

我的团队已经在这个问题上被困了一段时间,并且不知道接下来要去哪里尝试。 下面的规范在单独运行时可以正常工作,但是,当我们通过bundle exec ./bin/rspec spec在我们的套件中运行它时,这两个测试每次都会失败: GET / external-products /:id / deals GET /外部产品/搜索/交易 我们已经尝试了许多不同的方法来解决这个问题,我开始怀疑上述规范之外的其他内容。 所以我必须转向堆神,并请求那里有人有更好的方法,甚至更好的问题来问这个问题。 Rspec错误: 8) Retailigence Products and Locations GET /external-products/search/deals Search a given region for related deals by query string Failure/Error: expect(response_body).to have_json_type(Integer).at_path(‘deals/0/id’) JsonSpec::MissingPath: Missing JSON path “deals/0/id” # ./spec/features/external_products_spec.rb:151:in `block (3 levels) in ‘ # -e:1:in `’ 9) Retailigence Products and […]

使用布局响应所有HTML请求

我正在开发一个Rails应用程序,该应用程序将使用具有自己的路由function的客户端框架。 我想使用pushState路由,因此需要配置Rails路由器来响应这些请求(很简单)。 是否有一种简单的方法来设置具有有效路径的所有HTML请求,仅使用布局来响应,而不必使用一堆空白的action.html.erb文件来混淆我的视图文件夹?

Ruby on Rails CSV put&quot;&quot; 而不是实际的报价

我正在尝试生成CSV文件。 一切都很好,除了空白字段,我不太确定"" 而不是实际的报价。 我提供了用于生成文件和一些输出的代码。 <% row < <% row < <% row < 产量 Username,Name,E-mail,Phone Number admin,LocalShopper ,shoplocally1@gmail.com,"" Brian,Oliveri Design ,brian@oliveridesign.com,727-537-9617 LocalShopperJenn,Jennifer M Gentile ,localshopperjenn@hotmail.com,""

使用Pundit的各种角色的索引视图限制

我正在尝试为三个角色创建一个show view。 管理员,超级用户和用户。 管理员应该看到所有用户。 超级用户应该只看到用户,用户不应该看到任何人。 当我在解析else user.super_user?时使用注释掉的策略方法else user.super_user? 会给我unsupported: TrueClass错误。 欢迎任何建议。 用户控制器 def index @users = policy_scope(User) authorize User end 用户政策 class UserPolicy attr_reader :current_user, :model def initialize(current_user, model) @current_user = current_user @user = model end class Scope attr_reader :user, :scope def initialize(user, scope) @user = user @scope = scope end def resolve if user.admin? […]

广告过滤服务器端

我正在开发一个Web应用程序,我在其中显示来自其他网站的HTML。 在显示最终版本之前,我想摆脱广告。 关于如何实现这一点的任何想法,建议? 它不需要是一个超级高效的过滤工具,我在考虑将adblockplus定义的一些filter移植到Ruby,并在Nokogiri的帮助下返回解析的文档。 假设我使用超级通配符filterad 。 这不是官方adblock,但为了简单起见,我会在这里使用它。 然后想法是删除任何属性与filter匹配的所有元素,例如: src=”http://ad.foo.com?my-ad.gif” href=”http://ad.foo.com” class=”annoying-ad”等 此filter的Nokogiri命令是: doc.xpath(“//*[@*[contains(., ‘ad’)]]”).each { |element| element.remove } 我为此页面应用了filter: 结果是: 不是那么糟糕,请注意全局通配符filter也删除了有效的元素,如标题,因为它们具有id=”masthead”等属性。 所以我认为这种方法对我的情况来说是好的,现在的问题是使用什么filter? 他们有一个巨大的filter列表 ,我不想迭代所有这些filter。 我正在考虑抓住前10-20并根据它解析文档,那里有最受欢迎的列表吗? 如果是这样,我一直无法找到它。

Ruby on Rails:根据数据库排序规则比较两个字符串

我有一个单词列表,想要找到数据库中已存在的单词。 我决定使用“SELECT word FROM table WHERE word IN(array_of_words)”而不是进行数十次SQL查询,然后遍历结果。 问题是数据库整理。 http://www.collat​​ion-charts.org/mysql60/mysql604.utf8_general_ci.european.html 有许多不同的字符,MySQL视为相同。 但是,在Ruby代码中,string1不等于string2。 例如:如果单词是“šuo”,数据库也可能返回“suo”,如果找到了(并且没关系),但是,当我想检查时,如果找到“šuo”的东西,Ruby,当然,返回false(šuo!= suo)。 那么,有没有办法在相同的排序规则中比较Ruby中的两个字符串?

将Rails 3转换为Rails 2:使用块的帮助程序

在Rails 3中,我使用以下帮助程序来获得奇偶色表: def bicolor_table(collection, classes = [], &block) string = “” even = 0 for item in collection string < (((even % 2 == 0) ? “even ” : “odd “) + classes.join(” “))) do yield(item) end even = 1 – even end return string end 我在我的观点中使用它: 现在,我必须将应用程序迁移到Rails 2.问题是Rails 2不使用Erubis,因此当它找到标记时,它只调用whatever.to_s。 所以,在我的情况下,这导致尝试执行 (bicolor_table(services) do |service|).to_s 带来明显(坏)的后果。 […]

Rails多态与查找表

最好的方法是什么? 我希望能够通过多态性给乐队和艺术家带来流派。 我可以用habtm和has_many做到这一点:通过但是我试图弄清楚它是否可能通过多态性。 GenreList将是具有不同类型列表的查找表(例如Punk,Pop,Metal)。 我已经回顾了Ryan Bate关于Polymorphic Assoiciations的截屏video,但我仍然被卡住了。 具体来说,我不确定如何创建多态表Genre,它将从GenreList模型(查找表)中提供jar装类型。 以下是否正确? rails generate model Genre genre_list_id:integer genreable_id:integer genreable_type:string class Artist :genreable end class Band :genreable end class Genre true end class GenreList < ActiveRecord::Base end

Xcode 4是否支持ruby-on-rails?

Xcode 4是否支持rails项目? 如果是这样,到什么程度? 更新 – 显然它可以让你编辑文本文件。 但它是否为您提供任何IDEfunction,例如语法突出显示,导航到声明,代码完成等等?