简体   繁体   English

Ruby on Rails:如何在 _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

I have a marketplace application where users can create both products and categories我有一个市场应用程序,用户可以在其中创建产品和类别

In the product _form.html.erb a user can create a product and assign it one category.在产品_form.html.erb中,用户可以创建产品并为其分配一个类别。 The issue is that the category 'form.select' displays all categories that all users have created.问题是类别“form.select”显示了所有用户创建的所有类别。

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

The first thing you should do is make sure your associations are correct.您应该做的第一件事是确保您的关联是正确的。 You should have:你应该有:

category.rb类别.rb

belongs_to :user

user.rb用户.rb

has_many :categories

Once this is done, you should be able to use <%= form.select:category_id, current_user.categories.all.map {|c| [c.category, c.id]} %>完成此操作后,您应该可以使用<%= form.select:category_id, current_user.categories.all.map {|c| [c.category, c.id]} %> <%= form.select:category_id, current_user.categories.all.map {|c| [c.category, c.id]} %> , assuming you have access to current_user . <%= form.select:category_id, current_user.categories.all.map {|c| [c.category, c.id]} %> ,假设您可以访问current_user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM