【发布时间】:2017-03-26 02:07:38
【问题描述】:
模型/视频
class Video < ApplicationRecord
has_one :category, through: :video_category
has_one :video_category
end
型号/类别
class Category < ApplicationRecord
has_many :video_categories
has_many :videos, through: :video_categories
end
一个视频只能有一个类别,但一个类别可以有多个视频。
我让用户发布视频链接并让他们为每个视频选择最佳类别。我在管理员中创建了一些类别,它们只能使用我创建的类别。
观看次数/视频/新
<%= simple_form_for @new_video do |f| %>
<%= f.input :title %>
<%= f.input :description, as: :text %>
<%= f.input :category,
as: :select,
label: "Category" %>
<%= f.button :submit, "Submit", class:"btn btn-danger post-button-form" %>
<% end %>
没有类别,我只能在“是”或“否”之间进行选择 我不能使用 f.associations 而不是 f.input,因为我有一个错误说我不能使用具有“has_one”关系的关联。
我能做什么?我真的被困住了:(
谢谢
【问题讨论】:
标签: ruby-on-rails activerecord simple-form