在updatesgorm 上不会将布尔类型更新为false. 默认情况下它会更新为true,但是当我尝试更新为false不更改时。我也没有看到任何错误。可能是什么问题?
type Attendee struct {
ID uint `gorm:"primary_key" gorm:"AUTO_INCREMENT" json:"id,omitempty" mapstructure:"id" csv:"ID"`
Email string `json:"email,omitempty" mapstructure:"email" csv:"Email,required"`
ShowDirectory bool `json:"show_directory,omitempty" gorm:"default:true" mapstructure:"show_directory" csv:"-"`
}
var attendee Attendee
// JSON.unmarshal lines here for the &attendee
if err := service.DB.Model(&attendee).Updates(Attendee{
Email: attendee.Email,
ShowDirectory: false
}).Error; err != nil {
return Attendee{}, err
}
Run Code Online (Sandbox Code Playgroud)
替代解决方案:
这有效,但我正在更新多个属性。所以,我不能用这个。
att := Attendee{ID: 1}
service.DB.Model(&att).Update("ShowDirectory", false)
Run Code Online (Sandbox Code Playgroud) 我想了解更多有关Rails路由的信息.
会员和收藏
# Example resource route with options:
resources :products do
member do
get 'short'
post 'toggle'
end
collection do
get 'sold'
end
end
Run Code Online (Sandbox Code Playgroud)
命名空间和范围
# Example resource route within a namespace:
namespace :admin do
resources :products
end
scope :admin do
resources :products
end
Run Code Online (Sandbox Code Playgroud)
约束,Redirect_to
# Example resource route with options:
get "/questions", to: redirect {|params, req|
begin
id = req.params[:category_id]
cat = Category.find(id)
"/abc/#{cat.slug}"
rescue
"/questions"
end
}
Run Code Online (Sandbox Code Playgroud)
定制:
resources :profiles
Run Code Online (Sandbox Code Playgroud)
来自resource profiles编辑的原始网址.
http://localhost:3000/profiles/1/edit
Run Code Online (Sandbox Code Playgroud)
我想为只有点击的用户提供它,edit profile …
在启动服务器运行之前,应该配置应用程序主机以便运行应用程序,因为此应用程序支持多个用户的子域,转到etc/hosts文件并添加子域,如下所示:
sudo nano /etc/hosts
& add the text below at the end of you hosts file :
127.0.0.1 admin.daycare.no
127.0.0.1 daycare.no
127.0.0.1 worker.daycare.no
127.0.0.1 manager.daycare.no
127.0.0.1 parent.daycare.no
Run Code Online (Sandbox Code Playgroud)
这是要求,我这样做,但现在仍然如此
http://daycare.no:3000
Run Code Online (Sandbox Code Playgroud)
没跑,给我一个错误
**The following error was encountered while trying to retrieve the URL: http://daycare.no:3000/
Unable to determine IP address from host name daycare.no
The DNS server returned:
Name Error: The domain name does not exist.
This means that the cache was not able to resolve the hostname presented in the URL. Check …Run Code Online (Sandbox Code Playgroud) 我正在开发一个需要模型ActiveStorage has_many_attached :photos情况的项目Location.
我在下面设置了代码,但在尝试上传表单时,收到以下错误:
ActiveSupport::MessageVerifier::InvalidSignature in
LocationsController#attach_photo
Run Code Online (Sandbox Code Playgroud)
这是将文件"添加"到特定父记录(即:Location记录)的附件集的方法吗?
Location 模型class Location < ApplicationRecord
...
has_many_attached :photos
...
end
Run Code Online (Sandbox Code Playgroud)
class LocationsController < ApplicationController
...
def attach_photo
@location = Location.find(params[:id])
@location.photos.attach(params[:photo])
redirect_to location_path(@location)
end
...
end
Run Code Online (Sandbox Code Playgroud)
<%= form_tag attach_photo_location_path(@location) do %>
<%= label_tag :photo %>
<%= file_field_tag :photo %>
<%= submit_tag "Upload" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
resources :locations do
member do
post :attach_photo
end
end
Run Code Online (Sandbox Code Playgroud) 我只是想知道rails如何使用JSON中的activemodel-serializer渲染模型对象.我安装了activemodel-serializer但是输出有所不同.
理想是这样的:
"products": [
{
"id": 1,
"title": "Digital Portable System",
},
{
"id": 2,
"title": "Plasma TV",
}
]
Run Code Online (Sandbox Code Playgroud)
我的代码很简单;
class Api::V1::ProductsController < ApplicationController
before_action :authenticate_with_token!, only: [:create, :update, :destroy]
respond_to :json
def index
respond_with Product.all
end
def show
respond_with Product.find(params[:id])
end
def create
product = current_user.products.build(product_params)
if product.save
render json: product, status: 201, location: [:api, product]
else
render json: { errors: product.errors }, status: 422
end
end
def update
product = current_user.products.find(params[:id])
if product.update(product_params)
render json: product, …Run Code Online (Sandbox Code Playgroud) 我试图理解,函数中第1和第2次传递参数之间有什么区别.在这两种情况下,方法都是功能性和编译.
1)
generateReport(capacities...)
func generateReport(capacities ...float64) {
for i, cap := range capacities {
fmt.Printf("Plant %d capacity %.0f\n", i, cap)
}
}
Run Code Online (Sandbox Code Playgroud)
2)
generateReport(plantCapacities)
func generateReport(capacities []float64) {
for i, cap := range capacities {
fmt.Printf("Plant %d capacity %.0f\n", i, cap)
}
}
Run Code Online (Sandbox Code Playgroud)
找到了很少的好样品
2)Golang.org -以@Himanshu提到的方式传递论据.
我正在尝试为 AWS RDS Postgres 实例中的特定用户启用一些 Postgres 扩展。
1) 我尝试过使用 rails 迁移进行部署,但没有奏效。
class InstallPgTrgmContribPackage < ActiveRecord::Migration[5.1]
def change
enable_extension "fuzzystrmatch"
enable_extension "pg_trgm"
enable_extension "unaccent"
# execute "CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;"
# execute "CREATE EXTENSION IF NOT EXISTS pg_trgm;"
# execute "CREATE EXTENSION IF NOT EXISTS unaccent;"
end
end
Run Code Online (Sandbox Code Playgroud)
2)另外,尝试通过 ssh-ing 进入 postgres 并从那里创建它。
psql -h blabla.us-east-1.rds.amazonaws.com -p 5432 -U prod -d prod
prod=> CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
returns: ERROR: permission denied to create extension "fuzzystrmatch"
HINT: Must …Run Code Online (Sandbox Code Playgroud) ruby postgresql ruby-on-rails amazon-web-services amazon-rds
有没有人用过 random_slug ( https://github.com/josei/random_slug ) 作为friendly_id?它上次更新是在 5 年前,所以我不确定尝试它是否浪费时间,或者即使有更好的解决方案?
基本上,我使用friendly_id 来获取我帖子的标题,并且我有一个范围,因此这些帖子对用户来说是唯一的,但我非常希望这些帖子是随机生成的 URL,类似于 YouTube URL 的 I假设 - 这是否可以通过友好的 ID 进行,或者我是否以错误的方式处理这个问题,还有其他什么可以让我的生活更轻松 100 倍吗?
ruby ×4
go ×2
amazon-rds ×1
friendly-id ×1
go-gorm ×1
json ×1
postgresql ×1
routes ×1
routing ×1