当我验证模型时,出现以下错误
<ActiveModel::Errors:0x007ffecf1c9460
@base=#<Post id: nil, created_at: nil, label_id: nil, user_id: 3>,
@messages={:label=>["can't be blank"]},
@details={:label=>[{:error=>:blank}]}>
Run Code Online (Sandbox Code Playgroud)
如何自定义错误消息?
根据我的理解,以下应该有效
en:
activerecord:
errors:
models:
post:
attributes:
label:
blank: do not like it
Run Code Online (Sandbox Code Playgroud)
但是,我仍然收到默认错误消息。
我实现了一个反向代理来访问我的 Rails 应用程序。但是,每当我尝试登录时,ActionController::InvalidAuthenticityToken无论是管理员帐户还是非管理员帐户,我每次都会遇到错误。我读到你需要包含proxy_set_header X-Forwarded-Proto https;它才能工作,但到目前为止对我来说还没有。
这是我当前的 nginx conf 文件
upstream backend{
server localhost:3000;
}
server {
listen 80;
listen 443 ssl;
server_name localhost;
ssl_certificate localhost.cert;
ssl_certificate_key localhost.key;
location / {
root html;
index index.html index.htm;
proxy_pass http://backend;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Ssl on;
}
Run Code Online (Sandbox Code Playgroud)
当我使用 localhost:3000(这是我的 Rails 应用程序的主机名)登录时,它会登录。所以问题出在 nginx 上。这里还有我来自 Rails 的错误日志。
有什么建议如何解决吗?
编辑:更新了 nginx conf 文件
upstream backend{
server localhost:3000;
}
server {
listen 80;
listen 443 ssl;
server_name localhost;
ssl_certificate localhost.cert;
ssl_certificate_key localhost.key;
location …Run Code Online (Sandbox Code Playgroud) Rails 6 Action Mailbox 存在问题。我完成了安装的所有步骤,看起来设置没有任何问题。但是当我导航到 localhost:3000/rails/conductor/action_mailbox/inbound_emails 时,出现以下错误:
未定义方法“new_rails_conductor_inbound_email_path”#
我目前正在将 Rails 应用程序从 5.2.4 更新到 6.0.1。我使用了该任务,修复了一些折旧,然后一一rails app:update取消注释了 中的更改。new_framework_defaults_6_0.rb一切正常,所有测试都经过验证,因此我删除了该new_framework_defaults_6_0.rb文件并更改了配置以加载新的默认值,以及config.load_defaults 6.0... badaboum。
我可以启动 Rails 服务器,但 Puma 出现错误:
Puma caught this error: uninitialized constant Admin (call 'Admin.connection' to establish a connection)::Concerns
Did you mean? Concurrent (NameError)
/Users/xx/xx/app/models/admin.rb:4:in `<class:Admin>'
/Users/xx/xx/app/models/admin.rb:3:in `<top (required)>'
/Users/xx/.rvm/gems/ruby-2.6.5@xx/gems/zeitwerk-2.2.2/lib/zeitwerk/kernel.rb:16:in `require'
/Users/xx/.rvm/gems/ruby-2.6.5@xx/gems/zeitwerk-2.2.2/lib/zeitwerk/kernel.rb:16:in `require'
...
Run Code Online (Sandbox Code Playgroud)
如果我刷新,则会出现另一个错误:
Before process_action callback :authenticate_user has not been defined
Run Code Online (Sandbox Code Playgroud)
(该方法存在)
最后,如果我再次刷新:
undefined method `signed_in?' for #<#<Class:0x00007fc47a7706f0>:0x00007fc47a783a70>
Run Code Online (Sandbox Code Playgroud)
这些方法存在于我的代码中,而不是 Gem 中。就像某些文件未加载一样。我不知道问题是什么,config.load_defaults 6.0当存在时,它会做同样的事情,new_framework_defaults_6_0.rb那么为什么它适用于文件而不适用于config.load_defaults 6.0?
任何帮助将非常感激
红宝石 …
我在 Rails 6 应用程序中遇到以下错误,并且所有 javascript 均无法正常工作:
\n\nERROR in ./app/javascript/packs/application.js\nModule not found: Error: Can\'t resolve \'@rails/activestorage\' in \'/Users/jrsahuquillo/Dropbox/myappname/app/javascript/packs\'\n @ ./app/javascript/packs/application.js 9:0-31\n\nERROR in ./app/javascript/packs/application.js\nModule not found: Error: Can\'t resolve \'@rails/ujs\' in \'/Users/jrsahuquillo/Dropbox/myappname/app/javascript/packs\'\n @ ./app/javascript/packs/application.js 5:0-21\nRun Code Online (Sandbox Code Playgroud)\n\n我的package.json是:
{\n "name": "myappname",\n "private": true,\n "dependencies": {\n "@rails/actioncable": "^6.0.0-alpha",\n "@rails/activestorage": "^6.0.2",\n "@rails/ujs": "^6.0.2",\n "@rails/webpacker": "4.2.0",\n "bootstrap": "^4.4.1",\n "jquery": "^3.4.1",\n "jquery-ui": "^1.12.1",\n "jquery-ui-dist": "^1.12.1",\n "popper.js": "^1.16.0",\n "serialize-javascript": "2.1.2",\n "turbolinks": "^5.2.0"\n },\n "version": "0.1.0",\n "devDependencies": {\n "webpack-dev-server": "^3.8.0"\n },\n "optionalDependencies": {\n "fsevents": "2.0.0"\n …Run Code Online (Sandbox Code Playgroud) ruby-on-rails webpacker rails-ujs rails-activestorage ruby-on-rails-6
我正在尝试在 Rails 6 应用程序中使用 ActionText。我能够创建 blob,但是:
\n\n
\n 2. 我的“图像附件”在撰写邮件时看起来有点奇怪...但这也许是正常的?
我按照https://edgeguides.rubyonrails.org/action_text_overview.html上的说明进行操作。
\n\n我的 application.js 文件如下所示:
\n\n// This file is automatically compiled by Webpack, along with any other files\n// present in this directory. You\'re encouraged to place your actual application logic in\n// a relevant structure within app/javascript and only use these pack files to reference\n// that code so it\'ll be compiled.\n\nrequire("@rails/ujs").start()\nrequire("turbolinks").start()\nrequire("@rails/activestorage").start()\nrequire("channels")\n\n\n// Uncomment to copy all static images under …Run Code Online (Sandbox Code Playgroud) 我刚刚意识到我的列类型需要是小数而不是整数
我可以直接在 seeds.rb 中进行更改吗?还是我应该做其他事情?
另外,如果我打算在小数点后添加一个字母,我该怎么做?
例子: 2.0L, 2.5L, 5.7L, etc.
我尝试像以前在 Rails 5 中所做的那样组织我的前端。我有一些带有函数的 js 文件,并根据我的需要在不同的代码位置使用了这些函数。但是在 Rails 6 中使用 js 是完全不同的。不管怎样,我想我对包和 webpacker 有了主要的了解。但是如何使用自定义js函数呢?将其写入一个文件并在另一个文件中使用?应该有办法做到这一点。
例如,我有一些自定义的 js 包:
app/javascript/packs/custom_pack_with_functions.coffee:
console.log 'hey'
@hi = () ->
console.log 'HI'
Run Code Online (Sandbox Code Playgroud)
我希望该hi功能在我看来是可用的。
some_view.html.slim:
= javascript_pack_tag 'custom_pack_with_functions'
javascript:
hi()
Run Code Online (Sandbox Code Playgroud)
但是当我来到适当的页面时,我在控制台中只看到以下消息:
hey
ReferenceError: hi is not defined
Run Code Online (Sandbox Code Playgroud)
? 如何定义hi函数以从任何地方使用它?
相关/固定: Ruby on Rails:表单对象验证不起作用
我有以下验证..
validates :age, numericality: { greater_than_or_equal_to: 0,
only_integer: true,
:allow_blank => true
}
Run Code Online (Sandbox Code Playgroud)
不是必须的,如果输入需要是数字。我注意到如果有人输入单词而不是数字,则提交并通过验证后字段值会更改为 0。我希望它是空白的或输入的值。
仍然没有解决方案,但这里有更多信息。
rspec测试 it "returns error when age is not a number" do
params[:age] = "string"
profile = Registration::Profile.new(user, params)
expect(profile.valid?).to eql false
expect(profile.errors[:age]).to include("is not a number")
end
Run Code Online (Sandbox Code Playgroud)
Rspec 测试失败:
Registration::Profile Validations when not a number returns error when age is not a number
Failure/Error: expect(profile.errors[:age]).to include("is not a number")
expected [] to include "is not a number"
Run Code Online (Sandbox Code Playgroud)
2.6.5 :011 …Run Code Online (Sandbox Code Playgroud) 我目前正在将旧的 Rails 应用程序迁移到 Rails 6。
好像项目中有些文件和里面定义的类不一致。运行应用程序测试时我没有看到此错误,但在部署后我收到如下错误:
Zeitwerk::NameError: expected file /app/my?_/app/lib/multi_io.rb to define constant MultiIo, but didn't
Run Code Online (Sandbox Code Playgroud)
在以前的 SO 问题中,我可以找到以下建议:
config.autoloader = :classicconfig.autoload_path我不是在寻找解决方法,所以我更正了引发异常的文件。
我想找到一种编程方式来检测所有不一致的路径/类名,而无需在生产模式下运行应用程序。
到目前为止,我想到了三个选项:
在不需要多次迭代/部署的情况下验证我的代码的建议方法是什么?
非常感谢你提前
ruby-on-rails-6 ×10
ruby ×2
webpacker ×2
actiontext ×1
activerecord ×1
csrf ×1
javascript ×1
nginx ×1
rails-i18n ×1
rails-ujs ×1
zeitwerk ×1