标签: ruby-on-rails-6

在 Rails 6 中使用 activestorage 时,如何在重新显示表单时保留文件?

在 Rails 6 中,我有一个带有文件字段的表单,我使用 activestorage 来存储文件。如果提交表单后验证失败,则会重新显示表单,显示验证错误。重新显示表单时如何保留添加到文件字段的文件,以便用户不必再次将文件添加到表单?

rails 5 已经有一个类似的问题:Active Storage: Best practice to retain/cache Uploaded file when form redisplays,但是那里的解决方案只适用于 Rails 5。

ruby-on-rails rails-activestorage ruby-on-rails-6

7
推荐指数
1
解决办法
1842
查看次数

如何使用 RSpec 测试 ActionText?

我正在尝试编写一个 RSpec 系统测试,该测试涉及在页面上填写 ActionText / Trix 字段。

似乎此处ActionText::SystemTestHelper定义的可用于帮助完成此任务,但到目前为止我还没有成功实现它。

我的 RSpec 测试如下所示:

# spec/system/add_job_posting_spec.rb

require 'rails_helper'

RSpec.describe 'adding a job posting', type: :system do
  let(:user) { FactoryBot.create :user }

  before do
    login_as(user)
    visit new_job_posting_path
  end

  context 'with valid information' do
    let(:valid_job_posting) { FactoryBot.create :job_posting }

    scenario 'creates a job posting', js: true do
      fill_in 'Title', with: valid_job_posting.title
      fill_in_rich_text_area 'Description', with: 'Job Info'

      click_button 'Submit'
      expect(page).to have_content 'Job was successfully created.'
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我还尝试创建一个 RSpec 支持文件

# spec/support/action_text.rb …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails trix ruby-on-rails-6 actiontext

7
推荐指数
2
解决办法
1034
查看次数

从 Rails 6 中删除 ActiveStorage 路由

我从 ActionStorage 中删除了config/application.rb但我在运行时看到了这些路由rake routes | less

我没有设置我的/config/routes.rb...

                   Prefix Verb URI Pattern                                                                              Controller#Action
       rails_service_blob GET  /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET  /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET  /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT  /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create
Run Code Online (Sandbox Code Playgroud)

有谁知道我如何删除这些?

ruby-on-rails ruby-on-rails-6

7
推荐指数
3
解决办法
3460
查看次数

如何修复错误?' ....rubocop- ...显示 API 中没有的依赖项 ...”

我该如何修复这个错误?

+bundle install --jobs 3 --full-index --retry 3 --deployment
Warning: the running version of Bundler (1.17.2) is older than the version that created the lockfile (1.17.3). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
Fetching source index from https://rubygems.org/
Fetching https://github.com/QueueClassic/queue_classic.git
Fetching https://github.com/matthewd/websocket-client-simple.git
Fetching rake 13.0.0
Installing rake 13.0.0
Fetching concurrent-ruby 1.1.5
Fetching minitest 5.12.2
Installing minitest 5.12.2
Installing concurrent-ruby 1.1.5
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching zeitwerk 2.2.0
Fetching builder 3.2.3 …
Run Code Online (Sandbox Code Playgroud)

ruby bundler ruby-on-rails-6

7
推荐指数
1
解决办法
7056
查看次数

新 Rails 应用程序中的“获取原始源时出错”(Sprockets 4、Firefox 70)

sprockets 4.0.0 似乎与 Chrome 78 兼容,但与 Firefox 70 不兼容。

从一个新的 rails 应用程序开始:

rails -v # Rails 6.0.0
rails new helloworld
Run Code Online (Sandbox Code Playgroud)
# Gemfile
gem 'sass-rails', '~> 6' # not 5
Run Code Online (Sandbox Code Playgroud)
bundle | grep sprockets
Using sprockets 4.0.0
Using sprockets-rails 3.2.1
Run Code Online (Sandbox Code Playgroud)
# layouts/application.html.erb
javascript_include_tag 'application'
Run Code Online (Sandbox Code Playgroud)
# app/assets/javascripts/application.js
console.log('line 1');
throw 'line 2';
Run Code Online (Sandbox Code Playgroud)
# app/assets/config/manifest.js
//= link_directory ../javascripts .js
Run Code Online (Sandbox Code Playgroud)
# Dev tools console, Firefox 70
line 1 application.source.js:1
uncaught exception: line 2 application.source.js:2
Error while fetching an original source: "http://localhost:3000/assets/application.source.js" is not in …
Run Code Online (Sandbox Code Playgroud)

firefox ruby-on-rails sprockets ruby-on-rails-6

7
推荐指数
0
解决办法
481
查看次数

Rails 6 和 Tailwind CSS 不会部署到 Heroku

我有一个 Rails 6 应用程序,它成功部署到 Heroku 并在 localhost:3000 上工作。

我通过yarn和webpack添加了tailwindcss。它在 localhost 上运行得很好,但不能在 heroku 上运行。当我运行时heroku logs出现以下错误

控制台错误日志

我已经阅读了所有 Heroku Rails 6 Webpacker 问题,并尝试了所有建议。没有任何效果。

  1. 我已经注释掉了<%= stylesheet_pack_tag %>......没有帮助
  2. 我已经extract_css: true在 webpacker.yml 文件中切换了......没有帮助
  3. 我跑了
    • heroku buildpacks:clear
    • heroku buildpacks:set heroku/nodejs
    • heroku buildpacks:add heroku/ruby ...没有帮助

有谁知道发生了什么?

我的 github 仓库是https://github.com/HundredBillion/enneagram

heroku yarnpkg webpacker tailwind-css ruby-on-rails-6

7
推荐指数
3
解决办法
1736
查看次数

$(...).tooltip 不是函数 rails 6 webpack

我曾尝试更改 bootstrap、jquery 和 popper 的版本,但没有成功。我不认为我使用了多个版本的 jquery。不知道哪里出错了。如果有人能帮我找到我遗漏的东西,那就太好了。

这是我的文件列表,

包.json:

{
  "name": "kf",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-free": "^5.12.1",
    "@rails/ujs": "^6.0.0",
    "@rails/webpacker": "4.2.2",
    "bootstrap": "^4.4.1",
    "jquery": "^3.4.1",
    "popper.js": "^1.16.1",
    "sweetalert2": "^9.8.2"
  },
  "version": "0.1.0",
  "devDependencies": {
    "webpack-dev-server": "^3.10.3"
  }
}
Run Code Online (Sandbox Code Playgroud)

配置/webpack/environment.js

const { environment } = require('@rails/webpacker');
const webpack = require('webpack');

environment.plugins.append(
  'Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery',
    Popper: ['popper.js', 'default']
  })
);

module.exports = environment;
Run Code Online (Sandbox Code Playgroud)

应用程序.js

require('@rails/ujs').start();
require('jquery/dist/jquery');
require('popper.js/dist/umd/popper');
require('bootstrap/dist/js/bootstrap');
require('@fortawesome/fontawesome-free');
require('./owl.carousel.min');
require('./fastclick');
require('./custom-script');
require('./return-to-top');

const Swal = require('sweetalert2');
window.Swal …
Run Code Online (Sandbox Code Playgroud)

javascript jquery webpack bootstrap-4 ruby-on-rails-6

7
推荐指数
1
解决办法
2610
查看次数

Home#index 中的 ActiveStorage::InvariableError

目前我正在尝试在 Rails 6 中显示.HEIC图像。我正在使用ActiveStorage ImageMagic来使用变体来显示jpg. 基本上我正在尝试使用

mogrify -format jpg myimage.heic
Run Code Online (Sandbox Code Playgroud)

在图像中进行显示jpg

我添加Rails.application.config.active_storage.variant_processor到 application.rb 中以便能够使用该变体。然而,它似乎打破了以下行:

 <%= image_tag post.image.variant(format: 'jpg'), class: "card-home__img" %>
Run Code Online (Sandbox Code Playgroud)

为什么不工作?

ruby-on-rails imagemagick rails-activestorage ruby-on-rails-6

7
推荐指数
2
解决办法
6033
查看次数

Rails 6 如何链接 asset/config/manifest.js 中的子文件夹

我刚刚升级到 Rails 6 并按照指南制作了这个清单文件:

//= link_tree ../fonts
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
Run Code Online (Sandbox Code Playgroud)

但 Rails 抱怨我没有添加资产/images/subfolder/

我如何链接到/images它及其下的所有内容?

ruby-on-rails asset-pipeline ruby-on-rails-6

7
推荐指数
1
解决办法
7156
查看次数

是否可以覆盖 Rails 6 中的 create_migration 模板?

我尝试将自定义文件放置在 lib 文件夹内的多个位置,但没有成功。

\n

我找到了 2017 年的 PR 来重新激活此行为,但未合并。\n https://github.com/rails/rails/pull/13972

\n

我看了一下\n https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb#L17

\n

看起来它还没有实现,但我不知道是否应该更改这个文件。

\n

I\xc2\xb4ve 找到了有关如何在较旧的 Rails 版本中执行此操作的资源,但是有没有办法覆盖 Rails 6 中的 Rails 模板?

\n

ruby-on-rails ruby-on-rails-6

6
推荐指数
1
解决办法
461
查看次数