小编Dan*_*G2k的帖子

自制:木桶“java”定义无效

最近更新我的木桶时,我开始收到以下错误:

Error: Cask 'java' definition is invalid: Token '{:v1=>"java"}' in header line does not match the file name.
Run Code Online (Sandbox Code Playgroud)

我在 MacOS 10.14.4 和我的 Homebrew 版本上:

? brew --version
Homebrew 2.1.2
Homebrew/homebrew-core (git revision 6811d1; last commit 2019-05-11)
Homebrew/homebrew-cask (git revision ba4e3; last commit 2019-05-11)
Run Code Online (Sandbox Code Playgroud)

这是输出brew cask upgrade --verbose --debug

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> Updated Formulae
angular-cli          atlassian-cli        libpqxx              terraforming
app-engine-python    elasticsearch        osm2pgrouting
asciidoctorj         jenkins-lts          postgresql

==> Casks with `auto_updates` or `version …
Run Code Online (Sandbox Code Playgroud)

homebrew homebrew-cask

17
推荐指数
2
解决办法
4586
查看次数

Rails 4.2:使用deliver_later和无表格模型

我正在尝试使用Rails 4.2的deliver_later方法设置联系表单.但是,我只能让deliver_now工作,因为deliver_later正在尝试序列化我的对象并且每次都失败.

这是我的设置:

messages_controller.rb

class MessagesController < ApplicationController
  def new
    @message = Message.new
  end

  def create
    @message = Message.new(params[:message])
    if @message.valid?
      ContactMailer.contact_form(@message).deliver_later
      redirect_to root_path, notice: "Message sent! Thank you for contacting us."
    else
      render :new
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

contact_mailer.rb

class ContactMailer < ApplicationMailer
  default :to => Rails.application.secrets['email']

  def contact_form(msg)
    @message = msg
    mail(:subject => msg.subject, from: msg.email)
  end
end
Run Code Online (Sandbox Code Playgroud)

message.rb

class Message
    include ActiveModel::Model
    include ActiveModel::Conversion

    ## Not sure if this is needed ##
    include ActiveModel::Serialization

    extend ActiveModel::Naming

    attr_accessor :name, …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails rails-activejob

15
推荐指数
2
解决办法
4699
查看次数

OpenSSL:不再支持 -pbkdf2 选项?

直到最近,我还可以使用以下命令加密/解密文件:

\n\n

加密:

\n\n

openssl enc -aes-256-cbc -pbkdf2 -in un_encrypted.yml -out encrypted.data

\n\n

解密:

\n\n

openssl enc -d -aes-256-cbc -pbkdf2 -in encrypted.data -out un_encrypted.yml

\n\n

我最近更新了我的 Homebrew 软件包,似乎-pbkdf2不再支持该选项?我无法让它工作,并且不断收到有关如何使用 openssl 命令的帮助提示(我\xe2\x80\x99 已经使用上述命令好几年了)。简单地删除该-pbkdf2选项会导致输出文件损坏。

\n\n

有谁知道如何再次解密文件?

\n\n
    \n
  • MacOS 卡塔琳娜 10.15.4
  • \n
  • OpenSSL 1.1.1g
  • \n
\n\n

提前致谢

\n

encryption openssl

10
推荐指数
1
解决办法
5779
查看次数

Rails 4:actions_as_taggable_on的text_field没有用逗号分隔标签

我试图让我的表单中的text_field部分以逗号分隔acts_as_taggable_on标签.现在,当我重新加载页面时,逗号消失,所以如果一个字段有两个或更多标签,它们就会成为一个大标签.例如,我得到"Tag1 Tag2 Tag3"而不是"Tag1,Tag2,Tag3".我在3.4.2上使用act-as-taggable-.

这是我的_form.html.erb部分:

<h2>Tags:</h2>
<p>Please separate the tags with a comma ','</p>

<% @article.tag_types.each do |tag| %>
  <div class="form-group">
    <strong><%= label_tag tag.to_s.titleize %></strong><br />
    <%= f.text_field "#{tag.to_s.singularize}_list".to_sym, :placeholder => "Comma-separated list of #{tag.to_s}", class: 'form-control' %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

每次重新加载编辑页面时,输入值都会以某种方式从已存在的标记中删除逗号,因此文本字段如下所示:

<input id="article_country_list" class="form-control" type="text" name="article[country_list]" value="China U.S.A." placeholder="Comma-separated list of countries">
Run Code Online (Sandbox Code Playgroud)

而不是value="China, U.S.A."应有的.

这是我的模型,article.rb:

class Article < ActiveRecord::Base
  acts_as_taggable_on :people, :cities, :countries, :other
end
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激 :)

谢谢!

ruby-on-rails acts-as-taggable-on

8
推荐指数
1
解决办法
1332
查看次数

Rails 4:让 Webpacker 与 CircleCI 一起工作

我正在尝试将 Webpacker 集成到我正在开发的 Rails 4 应用程序中。我已将 gem 添加到 Gemfile 并运行指令以设置使用 Webpacker 和 Vue.js 的所有内容。资产在开发中编译得很好,但是当我将其推送到我们的 CI 管道时,我看到 CircleCI 返回以下错误:

ActionView::Template::Error:         ActionView::Template::Error: Webpacker can't find application in /home/circleci/app/public/packs-test/manifest.json. Possible causes:
  1. You want to set webpacker.yml value of compile to true for your environment
  unless you are using the `webpack -w` or the webpack-dev-server.
  2. webpack has not yet re-run to reflect updates.
  3. You have misconfigured Webpacker's config/webpacker.yml file.
  4. Your webpack configuration is not creating a manifest.
  Your manifest contains: …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails circleci webpacker

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

如何使用 Rails 7 和 esbuild 设置 Vue.js

我使用 esbuild 设置了一个新的 Rails 7 项目:rails new my_project -j esbuild

\n

然后我添加了 Vue.js 和yarn add vue.

\n

我可以看到 Vue 已成功添加到我的package.json文件中:

\n
{\n  "name": "app",\n  "private": "true",\n  "dependencies": {\n    "@hotwired/stimulus": "^3.0.1",\n    "@hotwired/turbo-rails": "^7.1.1",\n    "autoprefixer": "^10.4.5",\n    "esbuild": "^0.14.38",\n    "postcss": "^8.4.13",\n    "tailwindcss": "^3.0.24",\n    "vue": "^3.2.33"\n  },\n  "scripts": {\n    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds",\n    "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

然后我添加import "./vue_controllers"到我的application.js文件中:

\n
// Entry point for the build script in …
Run Code Online (Sandbox Code Playgroud)

javascript ruby-on-rails vue.js esbuild

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

Rails 4:未缓存页面的小N + 1问题

我有一些代码生成N + 1数据库查询问题.

只有在未缓存页面时才会出现此问题.页面缓存后,添加.includes实际会导致不必要的数据库调用.我想知道如何解决这个问题.

我的applicaiton_helper.rb包含以下内容:

module ApplicationHelper
  def by(article)
    "By #{article.username} on #{article.created_at.strftime('%B %e, %Y')}"
  end
end
Run Code Online (Sandbox Code Playgroud)

我的article.rb包含:

class Article < ActiveRecord::Base
  belongs_to :user

  def username
    user.username
  end
end
Run Code Online (Sandbox Code Playgroud)

而我的articles_controller.rb包含:

class ArticlesController < ApplicationController
  def index
    @articles = user_signed_in? ? Article.all : Article.all.published.limit(13)
  end
end
Run Code Online (Sandbox Code Playgroud)

有问题的username方法是调用User模型的方法.如前所述,当页面尚未缓存时,这会导致by(article)辅助方法连续调用User模型而不需要任何预先加载.但是,由于我正在缓存我的观点,这种低效只会发生一次.如果我将articles_controller.rb更改为以下内容:

class ArticlesController < ApplicationController
  def index
    @articles = user_signed_in? ? Article.all.includes(:user) : Article.all.published.limit(13).includes(:user)
  end
end
Run Code Online (Sandbox Code Playgroud)

N + 1问题在第一页加载时消失,但是.includes在重新加载页面时我得到了一个不必要的东西.

知道如何解决这个小故障吗?

谢谢!

ruby-on-rails

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

使用 RSpec 在 Rails 引擎中测试代码

我正在尝试做一些看似简单但事实证明相当困难的事情。

我想使用 RSpec 为放在libRails Engine 目录中的类编写测试。

这正是我正在使用的步骤:

rails plugin new mygem -T --mountable --full --dummy-path=spec/dummy
Run Code Online (Sandbox Code Playgroud)

然后我cd mygem; vim mygem.gemspec

我将以下行添加到 mygem.gemspec 中:

s.add_development_dependency "rspec-rails"
Run Code Online (Sandbox Code Playgroud)

我跑bundle install; rails generate rspec:install

然后我编辑~/mygem/lib/mygem/engine.rb添加以下内容:

module Mygem
  class Engine < ::Rails::Engine
    isolate_namespace Mygem

    config.generators do |g|
      g.test_framework :rspec
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我在 lib 目录中创建一个非常简单的类,~/mygem/lib/mygem/something.rb

并添加以下内容:

module Mygem
  class Something
    def hi
      "hi"
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

创建测试文件~/mygem/spec/something_spec.rb

然后添加以下内容:

require 'rails_helper'

describe Mygem::Something do
  it 'says hi' do
    s …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails

5
推荐指数
1
解决办法
4350
查看次数

Rails 6:使用 GET 而不是 DELETE 链接到 destroy_user_session_path

我刚刚开始一个简单的 Rails 6.0.3.1 项目。

我正在设置 Devise 操作,但注销链接似乎不起作用。尽管我指定了方法,但它正在执行GET请求而不是:DELETE

- if user_signed_in?
  = link_to 'Logout',
    destroy_user_session_path,
    class: 'button is-light',
    method: :delete

Run Code Online (Sandbox Code Playgroud)

单击链接会产生:

显示 GET 请求的 Rails 错误消息

控制台中没有错误:

单击时没有控制台错误

我的app/javascript/packs/application.js看起来像这样:

- if user_signed_in?
  = link_to 'Logout',
    destroy_user_session_path,
    class: 'button is-light',
    method: :delete

Run Code Online (Sandbox Code Playgroud)

所以 Rails ujs 应该拦截链接点击并执行 a DELETE,但事实并非如此。

我的config/webpack/environment.js看起来像这样:

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
Run Code Online (Sandbox Code Playgroud)

我尝试在其他 SO 线程上寻找解决方案,但没有找到有效的方法。有些人建议使用按钮而不是链接,但我想要链接而不是按钮。

我通过yarn安装Bulma CSS框架后出现了这个问题:

const { environment } = require('@rails/webpacker')
const { VueLoaderPlugin } = require('vue-loader')
const vue = require('./loaders/vue')

environment.plugins.prepend('VueLoaderPlugin', new VueLoaderPlugin())
environment.loaders.prepend('vue', vue)
module.exports …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails webpacker rails-ujs

5
推荐指数
1
解决办法
833
查看次数

Rails 4:Google地图无法显示

我正试图在我的Rails 4应用程序上显示一个简单的Google Map.我选择静态包含代码,直到我开始工作.在我的_head.html.erb部分中,我有:

<head>
  <title><%= content_for?(:title) ? yield(:title) : t('website.name') %></title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= yield(:google_maps_api) if content_for?(:google_maps_api) %>
  <%= csrf_meta_tags %>
</head>
Run Code Online (Sandbox Code Playgroud)

然后在另一个视图中,我有一个名为_google_map.html.erb的部分执行以下操作:

<% content_for(:google_maps_api) do %>
<%= javascript_include_tag "https://maps.googleapis.com/maps/api/js?key=xyz&sensor=false" %>
<script type="text/javascript">
    function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(-34.397, 150.644),
      zoom: 8
    };
    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>
<% end %>

<div …
Run Code Online (Sandbox Code Playgroud)

google-maps ruby-on-rails turbolinks

3
推荐指数
1
解决办法
2619
查看次数

从 Rails 4 迁移到 Rails 5:使用 ActionDispatch::Request.parameter_parsers 和 config.middleware

我目前正在将一个应用程序从 Rails 4 迁移到 Rails 5。

我在尝试发出以下命令时遇到了以下问题bundle exec rspec spec

/Users/me/.rvm/gems/ruby-2.2.2/gems/actionpack-5.1.4/lib/action_dispatch/middleware/stack.rb:35:in `build': undefined method `new' for ActionDispatch::ParamsParser:Module (NoMethodError)
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/actionpack-5.1.4/lib/action_dispatch/middleware/stack.rb:99:in `block in build'
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/actionpack-5.1.4/lib/action_dispatch/middleware/stack.rb:99:in `each'
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/actionpack-5.1.4/lib/action_dispatch/middleware/stack.rb:99:in `inject'
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/actionpack-5.1.4/lib/action_dispatch/middleware/stack.rb:99:in `build'
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-5.1.4/lib/rails/engine.rb:508:in `block in app'
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-5.1.4/lib/rails/engine.rb:504:in `synchronize'
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-5.1.4/lib/rails/engine.rb:504:in `app'
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-5.1.4/lib/rails/application/finisher.rb:45:in `block in <module:Finisher>'
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `instance_exec'
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-5.1.4/lib/rails/initializable.rb:30:in `run'
        from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-5.1.4/lib/rails/initializable.rb:59:in `block in run_initializers'
        from /Users/me/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
        from /Users/me/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
        from /Users/me/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
        from /Users/me/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:347:in `block in …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails rails-upgrade

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

RSpec + Capybara:redirect_to 外部页面将我发送回 root_path

我正在尝试编写一个功能测试来检查转到某个路径是否会将用户重定向到外部网站。

为了在我的测试中禁止外部连接,我在我的spec_helper.rb 中有以下内容

require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
Run Code Online (Sandbox Code Playgroud)

我的规范做了类似的事情:

it 'redirects safely' do
  visit "/some/route"

  expect(page).not_to have_content 'MyWebsite'
end
Run Code Online (Sandbox Code Playgroud)

在我的 ApplicationController 中,我有一个before_action应该根据条件从外部重定向:

class ApplicationController < ActionController::Base
  before_action :redirect_to_external_website, if: :unsupported_path

  private

  def redirect_to_external_website
    redirect_to 'https://some.other.website'
  end

  def unsupported_path
    # Some conditions
  end 
end
Run Code Online (Sandbox Code Playgroud)

重定向在开发中按预期工作。

但是,当我运行规范时,我可以看到发生了两次重定向(redirect_to_external_website我认为该方法被命中两次),然后它返回到我的根路径。

知道我可能做错了什么吗?

提前致谢!

rspec ruby-on-rails capybara

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

RSpec + Sidekiq:NoMethodError:未定义的方法'jobs' MyImportJob

我正在尝试在 Rails 4.2.4 应用程序中为 RSpec + Sidekiq 编写一些规范,但遇到了一些问题。

我的代码如下所示:

class MyImportJob
  include Sidekiq::Worker
  sidekiq_options queue: :default

  def perform(params)
    # Do magic
  end
end
Run Code Online (Sandbox Code Playgroud)

和规范:

describe MyImportJob, type: :job do
  let(:panel) { create(:panel) }

  describe '#perform' do
    context 'unsuccessfully' do
      it 'raises ArgumentError if no panel param was passed' do
        expect {subject.perform_async()}.to raise_error(ArgumentError)
      end
    end

    context 'successfully' do
      it 'given a panel, it increases the job number' do
        expect {
          subject.perform_async(panel_id: panel.id)
        }.to change(subject.jobs, :size).by(1)
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

Failure/Error: …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails sidekiq

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