标签: ruby-on-rails-5

Rails 5.1路由:动态:动作参数

Rails 5.0.0.beta4在包含dynamic:action和:controller segments的路由上引入了弃用警告:

DEPRECATION WARNING: Using a dynamic :action segment in a route is deprecated and will be removed in Rails 5.1. 
Run Code Online (Sandbox Code Playgroud)

来自此PR的提交消息指出:

允许:controller和:通过config/routes.rb中的路径指定的操作值一直是Rails中导致安全性发布的许多问题的根本原因.鉴于此,最好将控制器和操作明确列入白名单,而不是试图将"坏"值列入黑名单或消毒.

你会如何将一组动作参数"列入白名单"?我的路由文件中有以下内容,它们提出了弃用警告:

namespace :integrations do
  get 'stripe(/:action)', controller: 'stripe', as: "stripe"
  post 'stripe/deactivate', controller: 'stripe', action: 'deactivate'
end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails-5

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

轨道连接池5

我有rails 5 rc1的这个问题.有没有人知道如何在环境文件中配置它以及rails 5活动记录的默认连接池大小是什么.

   Puma caught this error: could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use (ActiveRecord::ConnectionTimeoutError)
    /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:202:in `block in wait_poll'
    /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `loop'
    /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:193:in `wait_poll'
    /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:154:in `internal_poll'
    /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:278:in `internal_poll'
    /home/bsethi/.rvm/gems/ruby-2.2.2/gems/activerecord-5.0.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:148:in `block in poll'
Run Code Online (Sandbox Code Playgroud)

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

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

弃用警告:不推荐使用alias_method_chain

我将项目升级到Rails 5.当我运行时,rspec我收到警告

DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. 
From module, you can access the original method using super. 
(called from <top (required)> at /home/alex/projects/myproject/config/application.rb:13)
Run Code Online (Sandbox Code Playgroud)

application.rb中的失败行是:

Bundler.require(*Rails.groups)
Run Code Online (Sandbox Code Playgroud)

如何找出导致此弃用警告的原因以及如何消除错误?

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

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

导轨c不在导轨5中工作

在rails 5应用程序内的终端中使用命令

rails c
Run Code Online (Sandbox Code Playgroud)

抛出的错误如下所示.我不知道这在类似的问题中意味着什么,解决方案是使用弹簧停止.我也试过了,但没有它仍然给出同样的错误.如果有人能指出这里的错误,那将会很棒.

Running via Spring preloader in process 6457
/Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require': dlopen(/Users/AmanChawla/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-darwin15/readline.bundle, 9): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib (LoadError)
  Referenced from: /Users/AmanChawla/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-darwin15/readline.bundle
  Reason: image not found - /Users/AmanChawla/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-darwin15/readline.bundle
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /Users/AmanChawla/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/completion.rb:10:in `<top (required)>'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console.rb:3:in `<top (required)>'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /Users/AmanChawla/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:138:in `require_command!' …
Run Code Online (Sandbox Code Playgroud)

command-line ruby-on-rails rails-console ruby-on-rails-5

15
推荐指数
3
解决办法
4647
查看次数

如何从Rails 5日志的SQL部分中过滤敏感参数?

Rails 5提供参数过滤,我已经指定config.filter_parameters += ["my_token"]application.rb.

在dev(环境)模式下测试我的应用程序,我看到my_token从日志文件的请求行中正确过滤:

Started GET "/something?my_token=[FILTERED]"

但是,紧跟在后面的SQL日志行仍然以纯文本形式包含参数的值("SELECT stuff FROM things"等,my_token作为参数).

Rails 5是否提供了从日志文件的SQL部分过滤此原始值的方法?

我还在生产模式下运行我的应用程序,虽然日志文件更简洁,但它们仍然在生成的SQL语句的D类型日志行中显示未过滤的值.

我没有指定自定义日志设置 - 默认情况下,我的过滤器参数设置以外的所有设置.

我自己的搜索显示没有相关的讨论.也许我错过了什么?

谢谢!

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

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

如何将日期转换为使用Rails解析.xls文档的时间?

我正在使用Rails 5.我想使用下面的代码解析.xls(不要与.xlsx doc混淆)

  book = Roo::Spreadsheet.open(file_location)
  sheet = book.sheet(0)
  text = sheet.to_csv
  csv = CSV.parse(text)

  arr_of_arrs = csv
  text_content = ""
  arr_of_arrs.each do |arr|
    arr.map!{|v| v && v.to_f < 1 && v.to_f > 0 ? TimeFormattingHelper.time_as_str(v.to_f * 24 * 3600 * 1000) : v}
    text_content = "#{text_content}\n#{arr.join("\t")}"
  end
Run Code Online (Sandbox Code Playgroud)

这是我在上面引用的方法

  def time_as_str(time_in_ms)
    regex = /^(0*:?)*0*/
    Time.at(time_in_ms.to_f/1000).utc.strftime("%H:%M:%S.%1N").sub!(regex, '')
  end
Run Code Online (Sandbox Code Playgroud)

我遇到麻烦的一个方面是我的.xls doc中出现的单元格

24:08:00
Run Code Online (Sandbox Code Playgroud)

被处理为

1904-01-02T00:08:00+00:00
Run Code Online (Sandbox Code Playgroud)

使用上面的代码.如何解析我在屏幕上看到的值?也就是说,如何将日期值转换为时间值?

作为另一个Excel文档的示例,显示为的单元格

24:02:00
Run Code Online (Sandbox Code Playgroud)

正在被上面的代码解析为

1899-12-31T00:02:00+00:00
Run Code Online (Sandbox Code Playgroud)

xls ruby-on-rails roo-gem ruby-on-rails-5

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

Rails 5 db migration:如何修复ActiveRecord :: ConcurrentMigrationError

之前的rake db:rollback停滞不前.现在,在尝试新迁移时,我们收到以下错误:

rake aborted!
ActiveRecord::ConcurrentMigrationError: 

Cannot run migrations because another migration process is currently running.

/home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-    5.1.4/lib/active_record/migration.rb:1315:in `with_advisory_lock'
/home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/migration.rb:1148:in `migrate'
/home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/migration.rb:1007:in `up'
/home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/migration.rb:985:in `migrate'
/home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:171:in `migrate'
/home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/home/me/.rvm/gems/ruby-2.4.1/gems/rake-12.1.0/exe/rake:27:in `<top (required)>'
/home/me/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `eval'
/home/me/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Run Code Online (Sandbox Code Playgroud)

我们正在使用Postresql

ruby postgresql activerecord ruby-on-rails-5

15
推荐指数
3
解决办法
4233
查看次数

设置local:true作为Rails 5中form_with的默认值

我正在开发一个项目,我们不会使用ajax调用来提交表单,所以我需要local: true在项目中输入每个表单,如rails docs中所示:

:local - By default form submits are remote and unobstrusive XHRs. Disable remote submits with local: true.

有没有办法默认将local选项设置为true?

我们正在使用这样的Rails 5 form_with助手:

<%= form_with(model: @user, local: true) do |f| %>
    <div>
        <%= f.label :name %>
        <%= f.text_field :name %>
    </div>

    <div>
        <%= f.label :email %>
        <%= f.email_field :email %>
    </div>
    <%= f.submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

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

15
推荐指数
3
解决办法
4510
查看次数

用于Rails的NGINX配置5带有puma的ActionCable

我正在将Jelastic用于我的开发环境(尚未投入生产).我的应用程序与Unicorn一起运行,但我发现带有ActionCable的websockets并将其集成到我的应用程序中.

一切都在本地工作正常,但在部署到我的Jelastic环境(使用默认的NGINX/Unicorn配置)时,我在我的javascript控制台中收到此消息,我在访问日志中看不到任何内容

WebSocket connection to 'ws://dev.myapp.com:8080/' failed: WebSocket is closed before the connection is established.
Run Code Online (Sandbox Code Playgroud)

我曾经在我的本地环境中通过在配置文件中添加所需的ActionCable.server.config.allowed_request_origins来解决它.所以我仔细检查了我的开发配置,这没关系.

这就是为什么我想知道NGINX配置是否有特定的东西,除了在ActionCable git页面上解释的内容

bundle exec puma -p 28080 cable/config.ru
Run Code Online (Sandbox Code Playgroud)

对于我的应用程序,我在这里输入链接描述,但没有提及任何有关NGINX配置的内容

我知道带有ActionCable的websocket很新,但我希望有人能够在这方面给我一个带头

非常感谢

nginx jelastic ruby-on-rails-5 actioncable

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

在rails插件外无法访问ActionCable通道

我正在尝试创建一个暴露ActionCable频道的gem,但我无法让它工作.

这是我的宝石

# lib/my_channel.rb
class MyChannel < ActionCable::Channel::Base
  def wait(data)
    # logic ...
  end
end

# lib/engine.rb
module MyApp
  class Engine < ::Rails::Engine
    isolate_namespace MyApp
  end
end
Run Code Online (Sandbox Code Playgroud)

然后我将gem添加到我的主应用程序中Gemfile,运行bundle install,启动控制台并运行MyChannel.哪个不产生和错误,意味着通道被正确包含.

然后我将其添加到我的主应用程序中

// application.js
var socket = "ws://localhost:3000/cable";
var cable = ActionCable.createConsumer(socket);
cable.subscriptions.create({ "channel": "MyChannel" }, {
  received: function(){
    // ...
  }
});
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误

Subscription class not found ({"command"=>"subscribe", "identifier"=>"{\"channel\":\"MyChannel\"}"})
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

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

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