当我选择多个类别时更新帖子,因为我传递了其他prametrs我的表关系
class Post < ActiveRecord::Base
has_many :post_categories
has_many :categories, :through => :post_categories
end
class Category < ActiveRecord::Base
has_many :post_categories
has_many :posts, :through => :post_categories
end
# post_id,
# category_id,
# lang - string(2) example "en" or "ru"
class PostCategory < ActiveRecord::Base
has_one :post
has_one :category
end
并形成
<div class="col-sm-5">
<%= f.select :category_ids, options_for_select(Category.all.collect { |c| [c.title, c.id] },@post.categories.collect { |c| c.id }), {:include_hidden => false}, {:multiple => true, :class => 'form-select form-control-static', :style => 'width: 100%'} %>
</div>
控制器
# Сохранение нового поста
def create
@post = Post.new(post_params)
# When create a post, I know how to pass parameter 3 to the bridge table, because I already have all the data in the links
@post.post_categories.each do |с|
с.lang = 'ru'
end
if @post.save
redirect_to post_path(@post)
else
render 'new'
end
end
def update
@post = Post.find(params[:id])
# How to pass parameters lang here, when updating the categories in the post?
if @post.update(post_params)
redirect_to post_path(@post)
else
render 'edit'
end
end
private
def post_params
params.require(:post).permit(:title, {:category_ids => []})
end
如何在帖子中更新类别时传递参数lang?