标签: ruby-on-rails-5

Rails 5:无法从参数中检索哈希值

我遇到了一个奇怪的问题.

undefined method `values' for #<ActionController::Parameters:0x007fb06f6b2728>
Run Code Online (Sandbox Code Playgroud)

是我得到的错误,当我将变量分配给param哈希,并尝试获取它的值.

attributes = params[:line_item][:line_item_attributes_attributes] || {}
attributes.values
Run Code Online (Sandbox Code Playgroud)

该参数看起来像哈希的散列:

{"0"=>{"product_attribute_id"=>"4"}, "1"=>{"product_attribute_id"=>"7"}}
Run Code Online (Sandbox Code Playgroud)

现在,当我在控制台中执行此操作并将其分配给变量属性时,它可以完美地工作.所以我很难理解这里没有的东西 - 以及如何让它发挥作用.

ruby-on-rails ruby-on-rails-5

29
推荐指数
2
解决办法
4万
查看次数

Rails 5:不推荐使用参数"Gem.paths =`"中的数组值

基于actioncable-examples构建websockets app.启动服务器,运行rake等时,我不断收到消息:

Array values in the parameter to `Gem.paths=` are deprecated.
Run Code Online (Sandbox Code Playgroud)

这里没有看到解决方案,但确实找到了解决方案.我想在这里发布问题和答案,以便它可用.

ruby-on-rails ruby-on-rails-5

29
推荐指数
1
解决办法
7334
查看次数

在Rails 5中添加引用列迁移

用户有很多上传.我想在上传表中添加一个引用该用户的列.迁移应该是什么样的?

Rails 3的相关问题:Rails 3迁移:添加参考列?

Rails 4的相关问题:在Rails 4中添加引用列迁移

rails-migrations rails-activerecord ruby-on-rails-5

29
推荐指数
1
解决办法
2万
查看次数

Rails 5 ActionController :: InvalidAuthenticityToken错误

我有一个rails应用程序,我打算升级到rails 5.我正在使用devise(v4.2.0)和rails(v5.0.0).正如设计README.md文件中所建议的那样,我尝试将prevent_from_forgery移到before_filter之上但仍然在我尝试登录或更新我的错误时出现错误ActionController::InvalidAuthenticityToken

我的Application Controller

class ApplicationController < ActionController::Base
 protect_from_forgery with: :exception, prepend: true
 before_action :configure_permitted_parameters, if: :devise_controller?

  protected

   def configure_permitted_parameters
     devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
     devise_parameter_sanitizer.permit(:account_update, keys: [:name])
   end

end
Run Code Online (Sandbox Code Playgroud)

我的另一个BugController

class BugsController < ApplicationController
  protect_from_forgery prepend: true, with: :exception
  before_action :authenticate_user!
  before_action :set_bug, only: [:show, :edit, :update]

    def update
      respond_to do |format|
      if @bug.update(bug_params)
        format.html { redirect_to @bug, notice: 'Bug was successfully updated.' }
        format.json { render :show, status: :ok, location: @bug }
     else
        format.html { …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails devise ruby-on-rails-5

25
推荐指数
3
解决办法
5万
查看次数

是否还需要使用Rails系统规范的DatabaseCleaner?

从我读过的关于Rails 5.1新系统规范的所有内容中,我的理解是Rails现在在内部处理数据库事务.

来自Rspec的博客:"[之前]您的测试和您的测试代码无法共享数据库事务,因此您无法使用RSpec的内置机制来回滚数据库更改,而是需要像数据库更清洁的gem.使用系统测试,Rails团队已经做了艰苦的工作以确保不是这种情况,因此您可以安全地使用RSpec的机制,而无需额外的宝石."

我的经历不同:

  1. 升级到Rails 5.1后,我的Rspec功能测试全部通过.
  2. 我将'功能'规格重命名为'系统'规格.所有测试都通过了
  3. 我删除了Database Cleaner gem,并删除了所有引用rails_helper.rb.JS测试现在由于validates uniqueness错误而失败.非JS测试通过.

我的测试非常简单.

let(:subject) { page }
let(:user) { create :user, name: "TestUser" }
it "displays the user page", :js do
  visit user_path(user)
  it is_expected.to have_content "TestUser"
end
Run Code Online (Sandbox Code Playgroud)

数据库清理器禁用,:js => true我得到user named TestUser already exists.随着:js => false测试通过.

系统测试和rspec的当前情况如何?Rails是否在内部处理数据库事务,还是仍需要数据库清理程序?有没有人遇到这个,或者可以指向我相关信息?

rspec ruby-on-rails database-cleaner ruby-on-rails-5

25
推荐指数
1
解决办法
4939
查看次数

Omniauth-twitter与Rails 5停止工作!OAuth ::未经授权403禁止使用

一切都很好,Omniauth突然停止了工作.我没有进行任何代码更改.

我检查了Twitter应用程序设置:我有回调网址到我的主要生产网址,并且回拨网址已锁定为否.所有密钥都是正确的.

任何的想法?


的OAuth ::未经授权

403禁止

在此输入图像描述

ruby-on-rails devise omniauth ruby-on-rails-5 omniauth-twitter

25
推荐指数
2
解决办法
2412
查看次数

您必须在此lockfile中使用Bundler 2或更高版本

我正在使用heroku,每当我尝试推送我的应用程序时,此消息显示出来:

remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Ruby app detected
remote: 
remote:  !
remote:  !     You must use Bundler 2 or greater with this lockfile.
remote:  !
remote: /tmp/d20181109-104-g861yi/bundler-1.15.2/gems/bundler-1.15.2/lib/bundler/lockfile_parser.rb:108:in `warn_for_outdated_bundler_version': You must use Bundler 2 or greater with this lockfile. (Bundler::LockfileError)
remote:     from /tmp/d20181109-104-g861yi/bundler-1.15.2/gems/bundler-1.15.2/lib/bundler/lockfile_parser.rb:95:in `initialize'
remote:     from /app/tmp/buildpacks/b7af5642714be4eddaa5f35e2b4c36176b839b4abcd9bfe57ee71c358d71152b4fd2cf925c5b6e6816adee359c4f0f966b663a7f8649b0729509d510091abc07/lib/language_pack/helpers/bundler_wrapper.rb:130:in `new'
remote:     from /app/tmp/buildpacks/b7af5642714be4eddaa5f35e2b4c36176b839b4abcd9bfe57ee71c358d71152b4fd2cf925c5b6e6816adee359c4f0f966b663a7f8649b0729509d510091abc07/lib/language_pack/helpers/bundler_wrapper.rb:130:in `block in parse_gemfile_lock'
remote:     from /app/tmp/buildpacks/b7af5642714be4eddaa5f35e2b4c36176b839b4abcd9bfe57ee71c358d71152b4fd2cf925c5b6e6816adee359c4f0f966b663a7f8649b0729509d510091abc07/lib/language_pack/instrument.rb:18:in `block (2 levels) in instrument'
remote:     from /app/tmp/buildpacks/b7af5642714be4eddaa5f35e2b4c36176b839b4abcd9bfe57ee71c358d71152b4fd2cf925c5b6e6816adee359c4f0f966b663a7f8649b0729509d510091abc07/lib/language_pack/instrument.rb:40:in `yield_with_block_depth'
remote:     from /app/tmp/buildpacks/b7af5642714be4eddaa5f35e2b4c36176b839b4abcd9bfe57ee71c358d71152b4fd2cf925c5b6e6816adee359c4f0f966b663a7f8649b0729509d510091abc07/lib/language_pack/instrument.rb:17:in `block in instrument'
remote:     from …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails heroku ruby-on-rails-5 heroku-cli

25
推荐指数
8
解决办法
3万
查看次数

Rspec:如何验证记录是否已被删除?

我创建了一个简单的 Rspec 测试来验证创建的模型是否已被删除。但是,测试失败,因为模型仍然存在。任何人都可以提供有关如何确定记录是否确实被删除的任何帮助吗?

RSpec.describe Person, type: :model do

let(:person) {
    Person.create(
      name: "Adam",
      serial_number: "1"
    )
  }
  
  it "destroys associated relationships when person destroyed" do
  person.destroy
  expect(person).to be_empty()
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby unit-testing rspec ruby-on-rails-5

25
推荐指数
2
解决办法
9670
查看次数

require_tree参数必须是Rails 5升级应用程序中的目录

我刚刚将我的应用程序升级Rails 4.2.7Rails 5.0.0.1.我使用RailsDiff来确保我已经覆盖了所有内容,我相信我做到了.到目前为止,一切都运行良好,直到加载我的应用程序.

现在我看到这个错误:

Sprockets::ArgumentError at /
require_tree argument must be a directory
Run Code Online (Sandbox Code Playgroud)

这是我的application.css:

/*
 * 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,
 * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails sprockets asset-pipeline ruby-on-rails-5

24
推荐指数
3
解决办法
7656
查看次数

如何使用 Turbo Streams 提交有关输入更改的表格?

我有一个表单,我想在任何输入字段发生更改时自动提交。我正在使用 Turbo Streams,如果我使用onchange: "this.form.submit()"它,它不会被 Turbo Streams 捕获,并且 Rails 使用标准 HTML 响应。单击提交按钮时效果很好。我该如何解决这个问题?

ruby-on-rails-5 hotwire-rails turbo

24
推荐指数
2
解决办法
1万
查看次数