Rails - collection_select 正在将输出转换为字符串而不是整数(并且不保存)
Rails - collection_select is converting output to a string instead of an integer (and not saving)
我在 rails 中的 collection_select 有问题,我似乎无法找到导致它的原因。我有 3 个模型、用户、项目和预算。
用户可以有多个项目,项目可以有一个预算。每个预算有一个项目。
我创建了一个 collection_select 字段来显示用户可能的预算列表 select,它工作正常,除了出于某种原因,它转换预算 'id'(一个整数)转换成一个字符串,然后在我的 'projects' table 的 'budget_id' 字段中将其保存为一个整数。我不明白为什么要这样做!任何帮助将不胜感激!
代码如下:
当我提交新的项目表单(正确显示)时从终端提取。据我所知,它正确地 select 编辑了预算项目的 ID select,但它以某种方式将其从整数转换为字符串,这就是它无法保存的原因?:
Started POST "/projects" for ::1 at 2016-08-23 22:24:10 +0200
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"DCY3kFtDVDguJUqznBDuJcXEZBqbsxz3Q1bCxNngkPIaccAFpl43vKD9308cOLlZboVcjvDu2FTTmLwI+9ERdw==", "project"=>{"description"=>"this is a test"}, "Project"=>{"budget_id"=>"4"}, "commit"=>"Create Project"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
SQL (0.5ms) INSERT INTO "projects" ("description", "created_at", "updated_at", "user_id") VALUES (, , , ) RETURNING "id" [["description", "this is a test"], ["created_at", 2016-08-23 20:24:10 UTC], ["updated_at", 2016-08-23 20:24:10 UTC], ["user_id", 1]]
(5.8ms) COMMIT
Redirected to http://localhost:3000/projects/27
Completed 302 Found in 16ms (ActiveRecord: 6.8ms)
这是预算、用户和项目的架构 tables:
create_table "budgets", force: :cascade do |t|
t.string "name"
t.integer "minimum"
t.integer "maximum"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "projects", force: :cascade do |t|
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "budget_id"
t.index ["user_id"], name: "index_projects_on_user_id", using: :btree
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.string "phone"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
从项目控制器中提取
def new
@project = current_user.projects.new
@budgets = Budget.all
end
def create
@project = current_user.projects.new(project_params)
if @project.save!
redirect_to @project, notice: 'Project was successfully created.'
else
render :new
end
end
def project_params
params.require(:project).permit(:user_id, :description, :budget_id)
end
从新项目的 _form 视图中提取
<div class="field">
<%= f.label :budget %>
<%= collection_select(:Project, :budget_id, @budgets, :id, :list_of_budgets) %>
</div>
项目模型
class Project < ApplicationRecord
belongs_to :user
has_one :budget
end
预算模型
class Budget < ApplicationRecord
belongs_to :project
def list_of_budgets
"#{name} (#{minimum} to #{maximum})"
end
end
我很茫然!感谢您的帮助!
表单参数始终以字符串形式提交 - rails 会处理。
您的问题是您使用了 :Project
而不是 :project
,因此该值最终为 params[:Project][:budget_id]
而不是 params[:project][:budget_id]
。
我在 rails 中的 collection_select 有问题,我似乎无法找到导致它的原因。我有 3 个模型、用户、项目和预算。
用户可以有多个项目,项目可以有一个预算。每个预算有一个项目。
我创建了一个 collection_select 字段来显示用户可能的预算列表 select,它工作正常,除了出于某种原因,它转换预算 'id'(一个整数)转换成一个字符串,然后在我的 'projects' table 的 'budget_id' 字段中将其保存为一个整数。我不明白为什么要这样做!任何帮助将不胜感激!
代码如下:
当我提交新的项目表单(正确显示)时从终端提取。据我所知,它正确地 select 编辑了预算项目的 ID select,但它以某种方式将其从整数转换为字符串,这就是它无法保存的原因?:
Started POST "/projects" for ::1 at 2016-08-23 22:24:10 +0200
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"DCY3kFtDVDguJUqznBDuJcXEZBqbsxz3Q1bCxNngkPIaccAFpl43vKD9308cOLlZboVcjvDu2FTTmLwI+9ERdw==", "project"=>{"description"=>"this is a test"}, "Project"=>{"budget_id"=>"4"}, "commit"=>"Create Project"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
SQL (0.5ms) INSERT INTO "projects" ("description", "created_at", "updated_at", "user_id") VALUES (, , , ) RETURNING "id" [["description", "this is a test"], ["created_at", 2016-08-23 20:24:10 UTC], ["updated_at", 2016-08-23 20:24:10 UTC], ["user_id", 1]]
(5.8ms) COMMIT
Redirected to http://localhost:3000/projects/27
Completed 302 Found in 16ms (ActiveRecord: 6.8ms)
这是预算、用户和项目的架构 tables:
create_table "budgets", force: :cascade do |t|
t.string "name"
t.integer "minimum"
t.integer "maximum"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "projects", force: :cascade do |t|
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "budget_id"
t.index ["user_id"], name: "index_projects_on_user_id", using: :btree
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.string "phone"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
从项目控制器中提取
def new
@project = current_user.projects.new
@budgets = Budget.all
end
def create
@project = current_user.projects.new(project_params)
if @project.save!
redirect_to @project, notice: 'Project was successfully created.'
else
render :new
end
end
def project_params
params.require(:project).permit(:user_id, :description, :budget_id)
end
从新项目的 _form 视图中提取
<div class="field">
<%= f.label :budget %>
<%= collection_select(:Project, :budget_id, @budgets, :id, :list_of_budgets) %>
</div>
项目模型
class Project < ApplicationRecord
belongs_to :user
has_one :budget
end
预算模型
class Budget < ApplicationRecord
belongs_to :project
def list_of_budgets
"#{name} (#{minimum} to #{maximum})"
end
end
我很茫然!感谢您的帮助!
表单参数始终以字符串形式提交 - rails 会处理。
您的问题是您使用了 :Project
而不是 :project
,因此该值最终为 params[:Project][:budget_id]
而不是 params[:project][:budget_id]
。