小编Die*_*zar的帖子

rails方法获取模型的关联名称

有没有办法找出模型的关联?拿这两个型号:

class Comment < ActiveRecord::Base
  belongs_to :commentable
end

class Post < ActiveRecord::Base
  has_many :comments
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

我正在寻找类似的东西:

Post.has_many #=> ['comments', ...]
Post.belongs_to # => ['user']
Comment.belongs_to # => ['commentable']
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails

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

这个JS代码片做了什么?`"发展"!=='生产'

这是react.js文件的全部内容:

if ("development" !== 'production') {
  var typeofSpec = typeof spec;
  var isMixinValid = typeofSpec === 'object' && spec !== null;

  "development" !== 'production' ? warning(isMixinValid, '%s: You\'re attempting to include a mixin that is either null ' + 'or not an object. Check the mixins included by the component, ' + 'as well as any mixins they include themselves. ' + 'Expected object but got %s.', Constructor.displayName || 'ReactClass', spec === null ? null : …
Run Code Online (Sandbox Code Playgroud)

javascript boolean

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

如何不忘记在代码中删除调试行

这在我看来是一个新颖的想法(因为我没有找到任何解决方案或任何人已经实现它)...

一个shell脚本,无论你何时git提交都会自动运行,或者如果你忘记删除项目中任何调试或开发环境的特定代码行,它们都会让你知道.

例如:

通常(在我的Ruby项目中)我会留下代码行来输出变量

puts params.inspect 
Run Code Online (Sandbox Code Playgroud)

要么

raise params.inspect
Run Code Online (Sandbox Code Playgroud)

此外,有时我会使用不同的方法,所以我可以很容易地看到效果,例如在使用delayed_job的情况下,我宁愿在开发期间没有延迟地调用方法.

问题是有时候我忘记更改这些方法或忘记删除调用以引发params.inspect,我会无意中推送该代码.

所以我想也许最简单的解决方案是为任何这样的调试行添加注释,例如

raise params.inspect #debug
Run Code Online (Sandbox Code Playgroud)

本质上将该行标记为仅开发/调试行.然后在一个shell脚本之前运行一些其他命令,比如git commit,它可以使用awk或grep来搜索#debug注释的所有最新修改文件并停止执行并提醒你.但是我不太了解shell脚本,所以我想我会请求帮助:)

debugging bash shell workflow zsh

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

为什么组合数组时Ruby的splat比使用+组合数组要慢?

我做了一个疯狂的猜测,将一个阵列溅到另一个阵列比将两个阵列加在一起要快,但在快速基准测试后我发现我错了.我假设解释器只是将splat转换为数组文字,并且不必+每次都调用它上面的方法.那么,为什么+比splat更快?

我使用了这个基准代码:

def test(trials = 1000)
  head = [1,2,3]
  tail = 100.times.to_a

  t = Time.now.to_f
  trials.times do |i| 
    a = [head, *tail]
  end
  puts "splat done in #{Time.now.to_f - t}"

  t = Time.now.to_f
  trials.times do |i| 
    a = head + tail
  end
  puts "+ done in #{Time.now.to_f - t}"
end
Run Code Online (Sandbox Code Playgroud)

我得到了这个结果:

2.2.5 :059 > test
splat done in 0.001013040542602539
+ done in 0.0009138584136962891
Run Code Online (Sandbox Code Playgroud)

增加试验:

2.2.5 :061 > test 1_000_000
splat done in 0.5123062133789062
+ done in …
Run Code Online (Sandbox Code Playgroud)

ruby performance

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

gem install pg错误:无法理解Yosemite w/Ruby 2.1.5上的kern.osversion`14.0.0'

我使用RVM安装Ruby-2.1.5并再次运行bundle.现在pg gem将无法安装,我收到此错误:

gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
Building native extensions with: '--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config'
This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    /Users/diego/.rvm/rubies/ruby-2.1.5/bin/ruby -r ./siteconf20141120-33258-108chh6.rb extconf.rb --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
Using config values from /Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... yes
checking for PQconnectionUsedPassword()... yes
checking for PQisthreadsafe()... yes
checking for PQprepare()... yes
checking for PQexecParams()... yes
checking for PQescapeString()... yes
checking for …
Run Code Online (Sandbox Code Playgroud)

ruby postgresql macos gcc

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

HMAC通过multipart/form-data文件上传验证API调用

我们有一个现有的API,第三方可以推送数据.API调用使用与Ruby的ApiAuth库中描述的相同的方案使用HMAC进行身份验证,我用它来验证签名的请求.我们现在需要支持multipart/form-data文件上传.

我正在尝试使用cURL编写一个bash脚本作为示例API调用.我已经有一个没有文件上传的工作(只有JSON数据的POST请求).

我坚持的部分是为multipart请求生成$ content_md5.我理解多部分请求的内容具有由边界字符串分隔的内容部分,如此处所述.

问题1:cURL生成它自己的边界字符串并将其附加到我的内容类型标题问题2:我应该MD5整个请求正文包含边界字符串和节标题吗?

所以基本上,我需要能够知道边界字符串是什么,以便我可以生成一个字符串,看起来像http多部分格式,第7行到第23行中描述的内容部分和边界,以便我可以MD5它.

有没有办法只用cURL做到这一点?有没有更好的方法来构建这样一个HMAC签名的多部分请求?

我希望用cURL做到这一点,作为第三方开发人员的一个通用示例,它将与我们的API集成,以便他们可以用他们正在使用的任何语言签署他们的请求.

authentication curl http hmac

5
推荐指数
0
解决办法
360
查看次数

使用 Webpack 向全局公开 Vue

我正在使用已部分迁移为使用 Webpacker 和 Vue 的旧版 Rails 应用程序。我们还有一个通过 CDN 加载的遗留脚本。该脚本也需要使用 Vue,但我们宁愿不将 Vue 捆绑到其中,而只使用旧版 Rails 应用程序中已有的 Vue。

我已经关注了这个问题如何将 Vue 公开为全局对象,我到了这一点,使用公开加载器,将 Vue 对象暴露在Vue.default. 本质上,暴露的“Vue”对象实际上是 aModule而真正的 Vue 对象嵌套在Vue.default.

我通过跟踪我能找到的所有文档和文章来达到这一点。这些是我能找到的最相关的:https : //bibwild.wordpress.com/2019/08/01/dealing-with-legacy-and-externally-loaded-code-in-webpacker/

这似乎不太理想,我想知道是否有一种“更正确”的方式直接暴露 Vue,即不嵌套在模块默认值中。

我试过的

我从头开始创建了一个新的 Rails 6 应用程序。通过以下方式添加JS依赖项后:

yarn install
yarn add vue
yarn add -D expose-loader
yarn add -D webpack-dev-serer
rails webpacker:install
Run Code Online (Sandbox Code Playgroud)

我添加了暴露加载器配置 config/webpack/development.js

environment.loaders.append('expose', {
  test: require.resolve('vue'),
  use: [{
      loader: 'expose-loader',
      options: 'Vue'
  }]
})
Run Code Online (Sandbox Code Playgroud)

然后我搭建了一个页面并将其作为根路由。

然后在 app/javascript/packs/application.js

environment.loaders.append('expose', {
  test: require.resolve('vue'),
  use: …
Run Code Online (Sandbox Code Playgroud)

javascript ruby-on-rails webpack vue.js

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

Google API OAuth 2.0 如何存储应用程序默认凭据

我正在构建与 Google URL Shortening API(使用 Ruby)的集成。根据他们有关使用 OAuth2 进行身份验证的说明,您需要定义一个环境变量,GOOGLE_APPLICATION_CREDENTIALS该变量是带有服务帐户凭据的 JSON/P12 文件的路径。

我想知道的是如何正确存储我的凭据而不将它们提交给源代码管理。我可以选择使用凭据提交 JSON 文件,因为存储库是私有的,但这听起来像是不好的做法。

ruby google-api google-authentication oauth-2.0

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

Laravel如何仅响应204代码状态而没有正文消息

我在用

return response(null,204);
Run Code Online (Sandbox Code Playgroud)

因为我想返回一个空的正文消息,但是问题是当我用红宝石代码解析响应时

JSON.parse(res.body)
Run Code Online (Sandbox Code Playgroud)

我得到一些身体信息:

{"data"=>[]}
Run Code Online (Sandbox Code Playgroud)

因此,如何避免返回此“数据”,而仅返回状态码?

php laravel

4
推荐指数
3
解决办法
5932
查看次数

RSpec控制器测试嵌套的强参数

我可能会或可能不会在RSpec代码中发现一个错误,该错误不允许在使用accepts_nested_attributes_for时根据需要发布嵌套属性。

这是我的控制器测试:

it 'attaches a file to document' do
  post :create, {
    app_id: @app1.id,
    document: {
      recipient_id: @app2.id,
      delivery_service: 'default',
      attachments_attributes: {
        0 => {
          attachment: fixture_file_upload('files/document.json', 'application/json')
        }
      }
    },
    format: 'json'
  }

  attachment = assigns(:document).attachments.first
  attachment.exists?.should be_true
  attachment.url.should match 'amazon'
end
Run Code Online (Sandbox Code Playgroud)

这是文档控制器的强项:

private

def document_params
  params.require(:document).permit(
    :recipient_id,
    :delivery_service,
    :document_id,
    :document_type,
    attachments_attributes:
      [:attachment, :attachment_file_name, :attachment_file_size, :attachment_content_type, :attachment_updated_at]
  )
end
Run Code Online (Sandbox Code Playgroud)

当测试发布到控制器时,attachments_attributes将被忽略,因为它无法识别0键。但这就是属性应有的方式,并且可以实时工作。

当我拿出0键并将其留在测试中时:

attachments_attributes: {
  attachment: fixture_file_upload('files/document.json', 'application/json')
}
Run Code Online (Sandbox Code Playgroud)

我懂了 undefined method '[]' for Tempfile

这是我的控制器的回溯信息:

# ~/.rvm/gems/ruby-2.0.0-p0/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb:40:in `method_missing'
# ~/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/nested_attributes.rb:452:in …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails

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