我正在尝试为称为分类的类别/视频帖子设置连接表。这一切似乎都很好,但是,当提交create video_form时,不会填充连接表。首先,我的表单下面有问题吗?我是否必须做任何其他事情才能知道选择字段实际上是针对该表的?
video_posts/_form.html.erb
<%= form_for('video_post', url: video_posts_path) do |f| %>
<%= f.text_field :video_title, placeholder: "Video Title" %>
<%= f.select :id, Category.all.collect { |c| [c.category_name, c.id] }, {:include_blank => true} %>
<%= f.text_area :video_description, placeholder: "Description" %>
<%= f.text_field :video_url, placeholder: "URL" %>
<%= f.submit "Post", class: "submit" %>
<% end %>
以下是我的模特。
class Categorization < ActiveRecord::Base
belongs_to :categories
belongs_to :video_posts
end
class Category < ActiveRecord::Base
attr_accessible :category_name
has_many :categorizations
has_many :video_posts, :through => :categorizations
end
class VideoPost < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
end
这是创建功能。
def create
post_paginate
@video_post = current_user.video_posts.build(params[:video_post])
if @video_post.save
flash[:success] = "Video posted!"
redirect_to root_path
else
flash[:success] = "Errors found! Please try again."
index
render :index
end
end
非常感谢大家。