小编Eye*_*dic的帖子

如何在 ruby​​ on Rails 中将字符串转换为字节数组

我想先将字符串转换为字节数组,然后才能使用私钥对其进行签名,但我以前没有这样做过。我的代码如下所示,任何人都可以告诉我如何做到这一点。谢谢。

require 'openssl'
require 'base64'
require 'digest/sha1'

@date = Date.today.strftime("%m/%d/%Y")
text_to_sign = "#{@order.phone_no}" + "#{@order.name}" + "#{@order.pay_type}" + "#{@order.pay_type}" + "1" + "MABIRA" + "81W30DI846" + "#{@date}" + "PULL" + "1" + "#{@cart.total_price}" + "#{@order.phone_no}" + ""
password = 'secret'

#converting the string to byte array
byte[] buff => new byte[1024]
text = buff.text_to_sign


private_key = OpenSSL::PKey::RSA.new(File.read('Private.key'), password)
ciphertext = private_key.private_encrypt(text)
ciphertext.encoding
signed_text = Base64.encode64(ciphertext).gsub("\n", '')
puts signed_text
signed_text
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

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

使用 User.create() 时,Rails 用户模型未分配 ID

我尝试寻找答案,但他们都使用 User.new 等而不是创建。

当在控制台中使用具有有效属性的 User.create() 时,我得到一个使用 nil 作为 ID 和 nil 作为时间戳创建的用户。

这是我的用户模型。

 class User < ApplicationRecord
  attr_writer :name, :email
  before_save {self.email = email.downcase}

  validates :username, presence:true,
                       length: {maximum: 30},
                       uniqueness:true


  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence:true,
                    length: {maximum:200},
                    format: {with: VALID_EMAIL_REGEX},
                    uniqueness: {case_sensitive: false}

  has_secure_password

  validates :password, presence: true,
                       length: {minimum:6}

end
Run Code Online (Sandbox Code Playgroud)

在我正在使用的 Rails 控制台中

User.create(username:"oiuedhioj", email:"damhan@dagr.com",password:"test",password_confirmation:"test")
Run Code Online (Sandbox Code Playgroud)

并回来

<User id: nil, username: "Damhan", email: nil, created_at: nil, updated_at: nil, password_digest: "$2a$10$oGtRgcHigaHh/UCVX4QdM.AOgyGur8Oud5MyKZheUcQ..."> 
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

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

Rails after_save和after_commit gotcha

after_save保存记录时(但在提交记录之前)调用.虽然after_create在提交数据库事务之后被调用了.

除了这篇文章http://www.justinweiss.com/articles/a-couple-callback-gotchas-and-a-rails-5-fix/解释Sidekiq作业无法找到对象,因为代码运行之前对象已经提交,为了决定使用after_create或者after_commit更适合使用,我需要知道的其他问题是什么?

ruby-on-rails

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

在Rails 5中更改我的默认localhost端口

我正在使用与默认的puma服务器一起工作的rails 5并收听localhost:3000

我希望它监听新的端口,例如192.168.0.0:3000

有人可以帮忙吗?谢谢

ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

3
推荐指数
2
解决办法
5176
查看次数

像 Rails ActiveRecord 这样的链接接口是如何构建的?

我很好奇。User.where(name: 'Foo').where(age: 20)仅使用一个数据库调用如何工作?

该方法是否where知道它是否是链的最后一个并改变其行为?如果是这样,如何做到这一点?

ruby ruby-on-rails rails-activerecord

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

Ruby on Rails API:计算属性模式最佳实践

当我需要需要调用数据库的计算属性时,我想知道最佳实践。

如果我有一个Parent有 many Child,我将如何渲染一个children_count属性,ParentController#index因为我不想渲染孩子,只是计数?最好的方法是什么?

谢谢!

模型:

class Parent < ApplicationRecord
  has_many :children

  def children_count
    children.count # Wouldn't it ask the database when I call this method?
  end
end
Run Code Online (Sandbox Code Playgroud)

控制器:

class ParentsController < ApplicationController
  def index
    parents = Parent.all

    render json: parents, only: %i[attr1, attr2] # How do I pass children_count?
  end
end
Run Code Online (Sandbox Code Playgroud)

design-patterns ruby-on-rails ruby-on-rails-6

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


PG::DatatypeMismatch:错误:AND 的参数必须是布尔类型,而不是整数类型

我使用 MySQL 在 Rails 中开发了一个基本应用程序。我正在转换为 PostgreSQL 数据库。我完成了数据库设置,除了有many to many关系的查询之外,大多数查询都可以工作。

我的模型看起来像这样:

User has_and_belongs_to_many :locations

Location has_and_belongs_to_many :users
Run Code Online (Sandbox Code Playgroud)

但是,当我执行这行代码时,出现以下错误:

location = current_user.locations.find_by(cookies[:current_location])
Run Code Online (Sandbox Code Playgroud)

产生这个错误:

PG::DatatypeMismatch: ERROR:  argument of AND must be type boolean, not type integer
LINE 1: ...n_id" WHERE "locations_users"."user_id" = $1 AND (1) LIMIT $...
                                                         ^
: SELECT  "locations".* FROM "locations" INNER JOIN "locations_users" ON "locations"."id" = "locations_users"."location_id" WHERE "locations_users"."user_id" = $1 AND (1) LIMIT $2
Run Code Online (Sandbox Code Playgroud)

提前致谢。

ruby-on-rails

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

Ruby:为什么所有坐标都在一个数组中更新?

我正在尝试更改特定的坐标,但是数组正在更新所有坐标。

目标是将fixed属性更改为单个坐标。

class Case
  attr_accessor :fixed

  def initialize
    self.fixed = false
  end

  def fixed?
    !!fixed
  end
end

def display(arr)
  5.times do |x|
    5.times do |y|
      print arr[x][y].fixed?
      print ' '
    end

    puts
  end
end

# Defining array
arr = Array.new(5){ Array.new(5, Case.new) }

# Displaying the arrays
display(arr)

# Changing value of a single coord
arr[2][3].fixed = true

# Displaying the arrays
display(arr)

Run Code Online (Sandbox Code Playgroud)

这是第一个显示呼叫的结果

false false false false false 
false false false false false 
false false false false …
Run Code Online (Sandbox Code Playgroud)

ruby

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

如何删除 React Web 应用程序中左上角的计时器计数器

我的反应应用程序左上角的时间隐藏了我的应用程序中的内容,我不知道如何删除它。

在此处输入图片说明

这是我截取的Gemfile. 与时间无关。

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.1.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

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