【发布时间】:2021-07-20 19:16:56
【问题描述】:
我正在关注这个tutorial,我正在开发一个应用程序,每个用户都有自己的植物列表。所以两张表:
- 表 A:植物:植物名称:字符串,温度:浮动,湿度:浮动,...
- 表 B:设计用户:默认参数来自 devise。
最初,表格 PLANTS 没有一列显示植物是由具有 id 的用户创建的。所以我使用以下步骤添加了它:
- 在 models/plant.rb 我添加了以下内容:
belongs_to :user - 在 models/user.rb 我添加了以下
has_many :plants - 在 GitBash 中:
rails g migration add_created_by_to_plants created_by:integer:index - 然后在 GitBash 中推送迁移:
rails db:migrate
所以现在架构看起来像这样:
ActiveRecord::Schema.define(version: 2021_04_27_111702) do
#Table Plants
create_table "plants", force: :cascade do |t|
t.string "plant_name"
t.float "temperature"
t.float "humidity"
t.float "water"
t.string "light"
t.string "soil"
t.string "location"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "created_by"
t.index ["created_by"], name: "index_plants_on_created_by"
end
#Table User Accounts
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.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end
我在创建新工厂时更改了前端,以便它可以在表单中提交由用户id创建:
<%= form.number_field :created_by, id: :plant_created_by, class:"form-control", value: current_user.id, type: :hidden %>
仅供参考,这显示了尝试创建新工厂的用户的正确 ID,并且应用程序按预期工作,直到教程中的 2:42:00! (应该得到用户必须存在错误,我这样做)
现在是我有错误的部分:
因为created_by是在之后创建的,所以我必须让我的controllers/plants_controller.rb知道它应该允许传递这个参数(2:43:00)
- 在
def plant_params中添加新参数:
def plant_params
params.require(:plant).permit(:plant_name, :temperature, :humidity, :water, :light, :soil, :location, :created_by)
end
但是当我尝试添加新工厂时,我仍然得到User must exist on front end,终端是这样的:
Started POST "/plants" for ::1 at 2021-04-27 15:34:18 +0300
Processing by PlantsController#create as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "plant"=>{"plant_name"=>"oregano", "temperature"=>"50", "humidity"=>"20.5", "water"=
>"150", "light"=>"Partial Shade", "soil"=>"Any", "location"=>"Anywhere", "created_by"=>"3"}, "commit"=>"Create Plant"}
Rendering layout layouts/application.html.erb
Rendering plants/new.html.erb within layouts/application
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 3], ["LIMIT", 1]]
↳ app/views/plants/_form.html.erb:43
Rendered plants/_form.html.erb (Duration: 5.7ms | Allocations: 1809)
Rendered plants/new.html.erb within layouts/application (Duration: 6.6ms | Allocations: 1892)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered home/_header.html.erb (Duration: 0.2ms | Allocations: 123)
Rendered layout layouts/application.html.erb (Duration: 64.9ms | Allocations: 6287)
Completed 422 Unprocessable Entity in 68ms (Views: 65.9ms | ActiveRecord: 0.2ms | Allocations: 8046)
【问题讨论】:
标签: ruby-on-rails database migration associations