Tag: ruby on rails 5

如何使用Rails和minitest模拟OmniAuth哈希?

我正在使用Rails 5和minitest。 我想模拟登录我的会话控制器,它依赖于omniauth(我使用谷歌和FB登录)。 我在我的控制器test,test / controllers / rates_controller_test.rb中有这个, class RatesControllerTest < ActionDispatch::IntegrationTest # Login the user def setup logged_in_user = users(:one) login_with_user(logged_in_user) end 然后我尝试在我的测试助手test / test_helper.rb中设置登录, class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all def setup_omniauth_mock(user) OmniAuth.config.test_mode = true omniauth_hash = { ‘provider’ => ‘google’, ‘uid’ => […]

Rails预编译资产未检测到Yarn可执行文件

我有一个Rails项目,我们从Rails 5.0转换为Rails 5.1.0。 当我尝试预编译资产时,我收到以下错误消息: Yarn executable was not detected in the system. Download Yarn at https://yarnpkg.com/en/docs/install 我从互联网搜索中读到的一切都说纱线和网络包装器附带Rails 5.1。 我从检查rails版本得到以下内容。 $rails –version Rails 5.1.0 我该如何解决? 我也想知道发生了什么。

Rails 5如何在多个共享属性上的表之间形成关联

在Rails 5中,给定两个表之间的关系,涉及将它们连接到多个共享属性上,如何在与这些表对应的模型之间形成关联? SQL: SELECT * FROM trips JOIN stop_times ON trips.guid = stop_times.trip_guid AND trips.schedule_id = stop_times.schedule_id 我尝试了以下配置,一般情况下… class Trip (trip){ where(“stop_times.schedule_id = ?”, trip.schedule_id) }, :inverse_of => :trip, :primary_key => :guid, :foreign_key => :trip_guid, :dependent => :destroy end class StopTime :stop_times, :primary_key => :guid, :foreign_key => :trip_guid end Trip.first.stop_times.first #> StopTime object, as expected […]

基于枚举值的自定义顺序

我为角色定义了Enum: enum role: {ordinary: 0, manager: 1, admin: 2} 我想按以下顺序订购一组对象: admin (first all admins) ordinary (then all ordinaries) manager (and lastly all managers) 这有可能吗?

传递给#or的关系必须在结构上兼容。 不兼容的值:

我有两个查询,我需要一个or它们之间,即我想要第一个或第二个查询返回的结果。 第一个查询是一个简单的where() ,它获取所有可用的项目。 @items = @items.where(available: true) 第二个包括join()并给出当前用户的项目。 @items = @items .joins(:orders) .where(orders: { user_id: current_user.id}) 我尝试以各种forms将这些与Rails’ or()方法结合起来,包括: @items = @items .joins(:orders) .where(orders: { user_id: current_user.id}) .or( @items .joins(:orders) .where(available: true) ) 但我一直遇到这个错误,我不知道如何解决它。 Relation passed to #or must be structurally compatible. Incompatible values: [:references]

Heroku:如果你在heroku中有多个应用程序,你如何推送到特定的应用程序?

我使用heroku create test-app3创建了一个新的heroku应用程序。 现在,如果我想将一些代码推送到这个新的test-app3 ,我该怎么做? 当我在终端输入heroku list时,我得到:我的应用程序 test-app1 test-app2 test-app3 我如何推送test-app3? 如何在要推送的应用程序之间切换?

使用Capistrano进行部署时,Rails 5控制台无法正常工作

我正在使用Rails 5,我已经通过Capistrano在服务器上部署了我的应用程序。 由于特定需要loadchema,我ssh in和cd到release / ###目录并尝试运行 rails –version#出来5.0.3beta 捆绑#工作,一切安装 rails c#但是失败了 运行rails db:migrate也失败了。 它似乎返回rails通用帮助,因为我的目录不是rails目录。 我尝试删除bin文件夹,但仍然相同。 有谁知道什么可能是错的? 谢谢

在Rails 5.0.0.beta3,-api选项中使用accepts_nested_attributes_for时出现问题

我正在使用Rails 5.0.0.beta3,使用rails new上的-app选项构建一个仅限API的应用程序,而我在使用accepts_nested_attributes_for时遇到了问题。 在我的应用程序中,具有嵌套属性的对象的创建(或新的,然后保存!)失败,并显示父父对象必须存在的消息。 为了测试,我制作了一个新的应用程序,只使用了ANAF文档中的成员和post的测试用例: class Member < ApplicationRecord has_many :posts accepts_nested_attributes_for :posts end 和 class Post < ApplicationRecord belongs_to :member end (这些类定义是由Rails脚手架生成器生成的,所以inheritance自ApplicationRecord,而不是ActiveRecord :: Base,但是根据这篇文章 ,这并不重要。) 对于已定义的类别,以及创建和运行的匹配迁移,我启动了Rails控制台并按照文档中的步骤操作: params = { member: { name: ‘joe’, posts_attributes: [ { title: ‘Kari, the awesome Ruby documentation browser!’ }, { title: ‘The egalitarian assumption of the modern citizen’ }, […]

rails 5 db:reset不工作

我想重置rails 5项目的数据库,但rails db:reset命令无效。 错误信息: Permission denied @ unlink_internal – C:/sites5/dawnrebirth/db/development.sqlite3 Couldn’t drop database ‘db/development.sqlite3′ rails aborted! Errno::EACCES: Permission denied @ unlink_internal – C:/sites5/dawnrebirth/db/development.sqlite3 bin/rails:4:in `require’ bin/rails:4:in `’ Tasks: TOP => db:drop:_unsafe (See full trace by running task with –trace)

`form_for`绕过了模型访问器。 如何让它停止? (或者:如何制作自定义属性序列化器?)

我将这些方法设置为自动加密值。 class User < ApplicationRecord def name=(val) super val.encrypt end def name (super() || '').decrypt end 当我尝试提交表单并且出现错误(缺少电话)时, name属性显示为乱码。 它在validation成功时起作用。 当我逐行通过我的控制器#update时,它也可以在控制台中工作。 irb(main):015:0> u = User.find 1 irb(main):016:0> u.name => “Sue D. Nym” irb(main):017:0> u.phone => “212-555-1234″ irb(main):018:0> u.update name: ‘Sue D. Nym’, phone: ” (10.0ms) BEGIN (1.0ms) ROLLBACK => false irb(main):020:0> u.save => false irb(main):029:0> u.errors.full_messages.join […]