Ruby on rails 如何将select2 rails与acts作为标记使用(简单表单)

Ruby on rails 如何将select2 rails与acts作为标记使用(简单表单),ruby-on-rails,simple-form,acts-as-taggable-on,select2-rails,Ruby On Rails,Simple Form,Acts As Taggable On,Select2 Rails,我需要允许用户为他们的帖子选择标签,但要从关闭列表中选择(假设只有管理员可以添加新标签,或者它们是以其他方式预定义的)。每个帖子可以有很多标签,所以它需要是多选的。不幸的是,添加select2 rails后,无法将标记保存在DB中。我怎样才能修好它?不适合我 PostsController: class PostsController < ApplicationController before_action :find_post, only: [:show, :edit, :upda

我需要允许用户为他们的帖子选择标签,但要从关闭列表中选择(假设只有管理员可以添加新标签,或者它们是以其他方式预定义的)。每个帖子可以有很多标签,所以它需要是多选的。不幸的是,添加select2 rails后,无法将标记保存在DB中。我怎样才能修好它?不适合我

PostsController:

class PostsController < ApplicationController
  before_action :find_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]

  def index
    @posts = Post.page(params[:page]).per(10)
    @tags = ActsAsTaggableOn::Tag.all
  end

  def new
    @post = current_user.posts.new
    @tags = ActsAsTaggableOn::Tag.all
  end

  def create
    @post = current_user.posts.new(post_params)
    if @post.save
      redirect_to @post, notice: _('Post created')
    else
      render :new, notice: _('Something went wrong')
    end
  end

  def show
    @tags = ActsAsTaggableOn::Tag.all
  end

  def edit
    authorize @post
    @tags = ActsAsTaggableOn::Tag.all
  end

  def update
    if @post.update(post_params)
      redirect_to @post, notice: _('Post updated')
    else
      render :edit, notice: _('Something went wrong')
    end
  end

  def destroy
    authorize @post
    if @post.destroy
      redirect_to root_path, notice: _('Post deleted')
    else
      redirect_to @post, notice: _('Something went wrong')
    end
  end

  private

  def post_params
    params.require(:post).permit(:title, :header, :content, :tag_list)
  end

  def find_post
    @post = Post.friendly.find(params[:id])
  end
end
def post_params
  params.require(:post).permit(:title, :header, :content, tag_list: [])
end
application.js:

$(document).ready(function(){
  $('.tags').select2({
    placeholder: 'Click to select',
    theme: 'bootstrap'
  });
});

嗯,这比我想象的要容易得多。我所要做的就是允许
tag\u list
作为数组
tag\u list:[]
(默认语法
:tag\u list
不会像前面描述的那样将标签保存在DB中),但只有这种更改导致了奇怪的行为—保存标签ID而不是名称。第二件事,在上面的链接中没有提到,是明确定义选择器的标签和值方法(这很重要,因为在默认情况下,它将传递标签ID,而不是标签名称,因此它将创建名称与以前标签ID相同的新标签)

在控制器中:

class PostsController < ApplicationController
  before_action :find_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]

  def index
    @posts = Post.page(params[:page]).per(10)
    @tags = ActsAsTaggableOn::Tag.all
  end

  def new
    @post = current_user.posts.new
    @tags = ActsAsTaggableOn::Tag.all
  end

  def create
    @post = current_user.posts.new(post_params)
    if @post.save
      redirect_to @post, notice: _('Post created')
    else
      render :new, notice: _('Something went wrong')
    end
  end

  def show
    @tags = ActsAsTaggableOn::Tag.all
  end

  def edit
    authorize @post
    @tags = ActsAsTaggableOn::Tag.all
  end

  def update
    if @post.update(post_params)
      redirect_to @post, notice: _('Post updated')
    else
      render :edit, notice: _('Something went wrong')
    end
  end

  def destroy
    authorize @post
    if @post.destroy
      redirect_to root_path, notice: _('Post deleted')
    else
      redirect_to @post, notice: _('Something went wrong')
    end
  end

  private

  def post_params
    params.require(:post).permit(:title, :header, :content, :tag_list)
  end

  def find_post
    @post = Post.friendly.find(params[:id])
  end
end
def post_params
  params.require(:post).permit(:title, :header, :content, tag_list: [])
end
鉴于:

= simple_form_for @post do |f|
  = f.input :title
  = f.input :header
  = f.input :content, input_html: { rows: 10 }
  = f.input :tag_list, collection: @tags, value_method: :name, label_method: :name, input_html: { multiple: true, class: 'tags' }
  = f.submit 'Save', class: 'btn btn-warning'
= link_to _('Back'), :back

什么是标签法?它与价值法有何不同?谢谢。@TimmyVonHeiss label_方法是您在选择器中显示的内容,value_方法是您传递给数据库保存的内容。e、 g.如果您有类别,并且帖子在db中有类别id,您希望在选择器中显示类别名称,并将类别id传递给db-您将标签方法设置为:name(类别名称),将值方法设置为:id(类别id)。