我刚刚阅读了有关在Docker中运行Rails开发环境的这篇文章.好文章,效果很好.设置完所有后,我决定继续设置生产环境.
目标:
我希望rake db:create && rake db:migrate每次运行我的泊坞窗图像.
问题:
如果我移动数据库创建和迁移步骤...
docker-compose run app rake db:create
docker-compose run app rake db:migrate
Run Code Online (Sandbox Code Playgroud)
...进入Dockerfile ...
RUN rake db:create && rake db:migrate
Run Code Online (Sandbox Code Playgroud)
......会引发错误......
could not translate host name "postgres" to address: Name or service not known
Run Code Online (Sandbox Code Playgroud)
...因为host我database.yml...
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
username: postgres
host: postgres
port: 5432
development:
<<: *default
database: rails_five_development
Run Code Online (Sandbox Code Playgroud)
...设置为postgres我的docker-compose.yml...中指定的服务名称
version: "2"
services:
postgres: …Run Code Online (Sandbox Code Playgroud) Firebase Firestore指南显示如何使用以下内容迭代集合快照中的文档forEach:
db.collection("cities").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
});
Run Code Online (Sandbox Code Playgroud)
我想它也会支持map,但事实并非如此.如何映射快照?
在控制器中,我想替换if..render..else..render为respond_with:
# Current implementation (unwanted)
def create
@product = Product.create(product_params)
if @product.errors.empty?
render json: @product
else
render json: { message: @product.errors.full_messages.to_sentence }
end
end
# Desired implementation (wanted!)
def create
@product = Product.create(product_params)
respond_with(@product)
end
Run Code Online (Sandbox Code Playgroud)
问题respond_with在于,如果出现验证错误,JSON 会以不符合客户端应用程序期望的特定方式呈现:
# What the client application expects:
{
"message": "Price must be greater than 0 and name can't be blank"
}
# What respond_with delivers (unwanted):
{
"errors": {
"price": [
"must be greater than 0"
],
"name": …Run Code Online (Sandbox Code Playgroud) json ruby-on-rails respond-with responders active-model-serializers
关于在使用create-react-app构建Chrome扩展程序时如何实现实时重新加载的任何建议?目前我yarn run build每次都有变化。
在一个字符串,我需要更换的所有出现[A],"A",'A'并_A_有[X],"X",'X'和_X_。我试过这个答案,但它给出了一个奇怪的结果(下面的代码)。我使用的new RegExp,因为它允许内插一个变量-A而X仅仅是一个例子。
// CAPTURE GROUPS: |------1-----|2|-----3------|
var regexp = new RegExp('(\'|\"|\_|\[)(A)(\'|\"|\_|\])', 'g')
var string = ' [A] _A_ "A" \'A\' A BAB "B" '
string.replace(regex, '$1X$3')
// ORIGINAL: [A] _A_ "A" 'A' A BAB "B"
// EXPECTED: [X] _X_ "X" 'X' A BAB "B"
// ACTUAL: [X] XXX XXX XXX X BXB XBX
Run Code Online (Sandbox Code Playgroud) 我知道我可以跳过单个保存的验证,如下所示:
User.new(name: 'John').save(validate: false)
Run Code Online (Sandbox Code Playgroud)
但是,一次保存多个对象时该怎么办?像这样:
Category.create([
{ name: 'Apps' },
{ name: 'Songs' },
{ name: 'Movies' }
])
Run Code Online (Sandbox Code Playgroud) 我想根据参数创建一个可以是单数或复数的句子count:
# When count is 1
"This profile still contains 1 post"
# When count is 2
"This profile still contains 2 posts"
Run Code Online (Sandbox Code Playgroud)
使用 Rails i18n 机制,我相信我必须嵌入 Ruby 代码才能获得“post”一词的正确复数形式。我正在尝试像这样构建它,但它不起作用:
# config/locales/en.yml
en:
message: "This profile still contains %{count} <%= Post.model_name.human(count: count).lowercase %>"
# Output of I18n.translate(:message, count: 2)
"This profile still contains 2 <%= Post.model_name.human(count: count).lowercase %>"
Run Code Online (Sandbox Code Playgroud)
我已经尝试过<%= %>,%{ },#{ }并且{{ }}和所有的失败。
甚至可以在 i18n 文件中嵌入 Ruby 代码吗?如何?
activerecord ×1
database ×1
docker ×1
dockerfile ×1
firebase ×1
javascript ×1
json ×1
reactjs ×1
regex ×1
respond-with ×1
responders ×1