我升级到 Rails 7 和 Ruby 3.1。在尝试运行测试时,rspec我收到以下错误。我该如何修复它?
An error occurred while loading rails_helper.
Failure/Error: require File.expand_path('../config/environment', __dir__)
LoadError:
cannot load such file -- net/smtp
# .../gems/mail-2.7.1/lib/mail.rb:9:in `<module:Mail>'
# .../gems/mail-2.7.1/lib/mail.rb:3:in `<main>'
# .../bundler/gems/rails-6a0f6c4d70b1/actionmailbox/lib/action_mailbox/mail_ext.rb:3:in `<main>'
# .../bundler/gems/rails-6a0f6c4d70b1/actionmailbox/lib/action_mailbox.rb:3:in `<main>'
# .../bundler/gems/rails-6a0f6c4d70b1/actionmailbox/lib/action_mailbox/engine.rb:9:in `<main>'
# ./config/application.rb:11:in `<top (required)>'
# ./config/environment.rb:2:in `require_relative'
# ./config/environment.rb:2:in `<top (required)>'
# ./spec/rails_helper.rb:4:in `require'
# ./spec/rails_helper.rb:4:in `<top (required)>'
Run Code Online (Sandbox Code Playgroud)
(bootsnap 和 zeitwerk 被排除在回溯之外)
我创建了新的 Rails 7 项目rails new my_project,但在包含要由 Rails 处理的自定义 JS 文件时遇到问题。
我的“javascript/application.js”
import "@hotwired/turbo-rails"
import "controllers"
import "chartkick"
import "Chart.bundle"
import "custom/uni_toggle"
Run Code Online (Sandbox Code Playgroud)
我的自定义 JS 文件:“javascript/custom/uni_toggle.js”
function uniToggleShow() {
document.querySelectorAll(".uni-toggle").forEach(e => e.classList.remove("hidden"))
}
function uniToggleHide() {
console.log("uni toggle hide")
document.querySelectorAll(".uni-toggle").forEach(e => e.classList.add("hidden"))
}
window.uniToggleShow = uniToggleShow
window.uniToggleHide = uniToggleHide
Run Code Online (Sandbox Code Playgroud)
我在我的布局中使用<%= javascript_importmap_tags %>
和我的“config/importmap.rb”
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
Run Code Online (Sandbox Code Playgroud) 从 Rails 6 升级到 Rails 7 并在本地运行一些 ActiveStorage 方法时,我看到:
Could not open library 'vips.42': dlopen(vips.42, 0x0005): tried: 'vips.42' (no such file), '/usr/local/lib/vips.42' (no such file), '/usr/lib/vips.42' (no such file), '/Users/st/rails/myapp/vips.42' (no such file), '/usr/local/lib/vips.42' (no such file), '/usr/lib/vips.42' (no such file). (LoadError)
Could not open library 'libvips.42.dylib': dlopen(libvips.42.dylib, 0x0005): tried: 'libvips.42.dylib' (no such file), '/usr/local/lib/libvips.42.dylib' (no such file), '/usr/lib/libvips.42.dylib' (no such file), '/Users/st/rails/myapp/libvips.42.dylib' (no such file), '/usr/local/lib/libvips.42.dylib' (no such file), '/usr/lib/libvips.42.dylib' (no such file)
Run Code Online (Sandbox Code Playgroud)
GitHub 上有很多类似的问题:(例如这里)。
我不确定是否需要安装 ruby-vips …
更新到 ruby 3.1.2 和 Rails 7.0.2.3 后
启动 Rails 应用程序时出现以下错误:
`require': cannot load such file -- matrix (LoadError)
Run Code Online (Sandbox Code Playgroud)
可能的解决方案是什么,提前致谢。
我需要渲染从 API 收到的 html 代码。
在 Rails 6 中:我在控制器中执行此操作,并且运行良好。我调用了收到响应的 Web 服务,然后我被重定向到渲染生成的代码。美好的 !
class GatewayController < ApplicationController
def new
init_gateway_call
end
def create
call_gateway
render_gateway_response
end
private
...
def render_gateway_response
render(html: @gateway_response.message.html_safe)
end
end
Run Code Online (Sandbox Code Playgroud)
新的.html.erb:
<%= form_with url: gateway_path, local: true do |f| %>
...
<% end %>
Run Code Online (Sandbox Code Playgroud)
没有:create.html.erb
** 导轨 7 **
我调用网络服务。我得到了答案,但我的页面空闲,并且出现此错误。
错误:表单响应必须重定向到 FetchRequest.receive (application-0f0c10fb8f5683e32f) 的 FormSubmission.requestSucceededWithResponse (application-0f0c10fb8f5683e32fc53a93a8a323c328de61682ca16fb65a6a2b8a3ba5d087.js:1614) 的另一个位置c53a93a8a323c328de61682ca16fb65a6a2b8a3ba5d087.js:1390) 在 FetchRequest.perform (application-0f0c10fb8f5683e32fc53a93a8a323c328de61682ca16fb65a6a2b8a3ba5d087.js:第1374章)
到目前为止,我尝试过:
# GatewayController
respond_to :create, format: :html, gateway_response: @gateway_response.message.html_safe
Run Code Online (Sandbox Code Playgroud)
<%= gateway_response %>
Run Code Online (Sandbox Code Playgroud)
没有成功......你有什么想法吗?不然这将是一个漫长的周末^^
我对 Rails 很陌生。我是从 Rails 7 开始的,所以关于我的问题的信息仍然很少。
应用程序/模型/cocktail.rb
class Cocktail < ApplicationRecord
has_many :cocktail_ingredients, dependent: :destroy
has_many :ingredients, through: :cocktail_ingredients
accepts_nested_attributes_for :cocktail_ingredients
end
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/ingredient.rb
class Ingredient < ApplicationRecord
has_many :cocktail_ingredients
has_many :cocktails, :through => :cocktail_ingredients
end
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/cocktail_ingredient.rb
class CocktailIngredient < ApplicationRecord
belongs_to :cocktail
belongs_to :ingredient
end
Run Code Online (Sandbox Code Playgroud)
应用程序/控制器/cocktails_controller.rb
def new
@cocktail = Cocktail.new
@cocktail.cocktail_ingredients.build
@cocktail.ingredients.build
end
def create
@cocktail = Cocktail.new(cocktail_params)
respond_to do |format|
if @cocktail.save
format.html { redirect_to cocktail_url(@cocktail), notice: "Cocktail was successfully created." }
format.json { render :show, status: …Run Code Online (Sandbox Code Playgroud) 新生成的带有 esbuild 选项的 Rails 7.0 在启动时出错。
rails new [project name] --javascript=esbuild --css=tailwind
Run Code Online (Sandbox Code Playgroud)
在创建新的 Rails 7 项目时,我尝试使用bin/dev现在使用的 foreman 来启动应用程序。但是,它会出错,并显示“未找到错误命令“build””。
bin/dev
!10520
16:07:31 web.1 | started with pid 53018
16:07:31 js.1 | started with pid 53019
16:07:31 css.1 | started with pid 53020
16:07:32 js.1 | yarn run v1.22.17
16:07:32 css.1 | yarn run v1.22.17 **************
16:07:32 js.1 | error Command "build" not found. <== *****THIS*****
16:07:32 js.1 | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
16:07:32 css.1 | …Run Code Online (Sandbox Code Playgroud) application.js由于某些未知原因,当我在浏览器中按 F5 时,Rails 7(开发环境)不会自动获取更改。该位置application.js是默认的。我几乎使用默认设置。
当我运行服务器时,它从某个缓存版本中获取 JavaScript。我需要明确地rails assets:precompile使其发挥作用。
导入映射看起来标准:
# Pin npm packages by running ./bin/importmap
pin "application", preload: true
...
Run Code Online (Sandbox Code Playgroud)
布局文件似乎也很标准:
<!DOCTYPE html>
<html>
<head>
<title>Whatever</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="<%= image_path('favicon.svg') %>">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</head>
...
Run Code Online (Sandbox Code Playgroud)
config.importmap.sweep_cache = true我尝试在我的(根据 importmap 文档)中进行设置development.rb,但似乎没有效果。
此时我非常绝望,无法理解为什么我需要rails assets:precompile在我的开发环境中这样做。
另外,我没有在“localhost”域上运行我的应用程序,它是在类似dev.server-somewhere.com(因此可以从任何地方访问)的 SSH 重定向上运行我的应用程序,类似于 ngrok。不确定这是否是问题的原因。
澄清一下:我正在寻找没有实时刷新功能的功能,只需使用 F5 页面刷新的标准方法即可。
我将Rails应用程序升级到Rails 7。我知道Turbolinks和Rails UJS实际上被Rails 7中的Stimulus和Turbo的Hotwire组合所取代,但我想知道我是否仍然可以使用UJS,如果可以,为什么不可以在职的?
我的方法不起作用,如下所示:
submit(event) {
this.errorTarget.classList.add("hidden")
Rails.fire(this.formTarget, "submit")
console.log('hi')
}
Run Code Online (Sandbox Code Playgroud)
console.log 有效。当我单击某个元素时,它过去会随此代码而更改,但现在它不再更改。Rails.fire当我检查网站时,根本不再触发,并且日志或网络部分没有错误。
我觉得我在这里错过了一些重要的东西,但我不知道是什么。
我正在尝试通过安装 bootstrap 和 jQuery 创建一个 Rails 应用程序。首先我尝试使用创建
rails new name--css bootstrap
Run Code Online (Sandbox Code Playgroud)
但它不起作用。所以我用它手动完成了。
我也尝试使用 esbuild 但在控制台中打印时它不起作用。这是我尝试过的YouTube 链接。
所以问题是如何在不使用 webpacker 的情况下在 Rails 7 应用程序中安装 jQuery
问题是现在 bootstrap 和 JavaScript 可以工作,但 jQuery 不行。
这是我的文件看起来像
// app/assets/stylesheet/application.scss
@import "bootstrap-sprockets";
@import "bootstrap";
Run Code Online (Sandbox Code Playgroud)
// app/javascript/application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap.min
// require turbolinks
//= require_tree .
Run Code Online (Sandbox Code Playgroud)
# Gemfile
gem 'bootstrap-sass', '~> 3.3.7'
gem 'jquery-rails'
gem 'sass-rails'
Run Code Online (Sandbox Code Playgroud)
所有这些创建了一个控制器并添加了一些基本的引导程序、JavaScript 和 jquery 代码来测试它是否工作,但 JavaScript 和引导程序都工作。将 ajax.googleapi 链接添加到 HTML 页面时,jQuery 正在工作。但这不是一个好的做法。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
所以问题是如何在 …
ruby-on-rails-7 ×10
javascript ×2
import-maps ×1
jquery ×1
matrix ×1
rails-ujs ×1
ruby ×1
ruby-3.1 ×1