Rails 上的 Ruby:如何在 _form.html.erb 中编写 'form.select' 以仅显示用户创建的类别
Ruby on Rails: How to write 'form.select' in _form.html.erb to show ONLY the categories that the user has created
我有一个市场应用程序,用户可以在其中创建产品和类别
在产品 _form.html.erb
中,用户可以创建产品并为其分配一个类别。问题是类别 'form.select' 显示了所有用户创建的所有类别。
如何重写
<div>
<%= form.select :category_id, Category.all.map {|c| [c.category, c.id]} %>
</div>
只显示当前用户创建的分类?
--
<%= form_with(model: product, local: true) do |form| %>
<% if product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description %>
</div>
<div class="field">
<%= form.label :price %>
<%= form.text_field :price %>
</div>
<div>
<%= form.select :category_id, Category.all.map {|c| [c.category, c.id]} %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
但它列出了数据库中的所有类别。我该如何编写此代码,以便表单仅列出用户生成的类别。
数据库
create_table "categories", force: :cascade do |t|
t.string "category"
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["user_id"], name: "index_categories_on_user_id" end
create_table "products", force: :cascade do |t|
t.string "title"
t.text "description"
t.decimal "price", precision: 8, scale: 2
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "category_id"
t.index ["user_id"], name: "index_products_on_user_id" 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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "username"
t.string "name"
t.boolean "admin", default: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "uid"
t.string "provider"
t.string "access_code"
t.string "publishable_key"
t.string "stripe_id"
t.boolean "subscribed"
t.string "card_last4"
t.string "card_exp_month"
t.string "card_exp_year"
t.string "card_type"
t.text "perk_subscriptions", default: [], array: true
t.string "s_name"
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
你应该做的第一件事是确保你的联想是正确的。你应该有:
category.rb
belongs_to :user
user.rb
has_many :categories
完成后,您应该可以使用 <%= form.select :category_id, current_user.categories.all.map {|c| [c.category, c.id]} %>
,假设您可以访问 current_user
。
我有一个市场应用程序,用户可以在其中创建产品和类别
在产品 _form.html.erb
中,用户可以创建产品并为其分配一个类别。问题是类别 'form.select' 显示了所有用户创建的所有类别。
如何重写
<div>
<%= form.select :category_id, Category.all.map {|c| [c.category, c.id]} %>
</div>
只显示当前用户创建的分类?
--
<%= form_with(model: product, local: true) do |form| %>
<% if product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description %>
</div>
<div class="field">
<%= form.label :price %>
<%= form.text_field :price %>
</div>
<div>
<%= form.select :category_id, Category.all.map {|c| [c.category, c.id]} %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
但它列出了数据库中的所有类别。我该如何编写此代码,以便表单仅列出用户生成的类别。
数据库
create_table "categories", force: :cascade do |t|
t.string "category"
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["user_id"], name: "index_categories_on_user_id" end
create_table "products", force: :cascade do |t|
t.string "title"
t.text "description"
t.decimal "price", precision: 8, scale: 2
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "category_id"
t.index ["user_id"], name: "index_products_on_user_id" 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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "username"
t.string "name"
t.boolean "admin", default: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "uid"
t.string "provider"
t.string "access_code"
t.string "publishable_key"
t.string "stripe_id"
t.boolean "subscribed"
t.string "card_last4"
t.string "card_exp_month"
t.string "card_exp_year"
t.string "card_type"
t.text "perk_subscriptions", default: [], array: true
t.string "s_name"
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
你应该做的第一件事是确保你的联想是正确的。你应该有:
category.rb
belongs_to :user
user.rb
has_many :categories
完成后,您应该可以使用 <%= form.select :category_id, current_user.categories.all.map {|c| [c.category, c.id]} %>
,假设您可以访问 current_user
。