I have a marketplace application where users can create both products and categories. In the product's _form.html.erb
In the product _form.html.erb a user can create a product and assign it one category. The issue is that the category 'form.select' displays all categories that all users have created.
How can I rewrite
<div>
<%= form.select :category_id, Category.all.map {|c| [c.category, c.id]} %>
</div>
to display the categories only the current user has created?
--
<%= 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 %>
but it lists all the categories in the database. How do I write this code so the form lists only the categories the user has produced.
database
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
Aucun commentaire:
Enregistrer un commentaire