我正在尝试为称为分类的类别/视频帖子设置一个连接表。一切似乎都很好,但是,当提交 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
非常感谢大家。