如何使用acts_as_taggable_on(simple_form)的select2-rails

时间:2016-04-09 18:45:21

标签: ruby-on-rails simple-form acts-as-taggable-on select2-rails

我需要允许用户为他们的帖子选择标签,但是从封闭列表中选择(让我们说只有管理员可以添加新标签,或者以其他方式预定义)。每个帖子都可以有很多标签,所以它需要是多选择器。不幸的是,添加select2-rails标签后,无法保存在DB中。我该如何解决? This对我不起作用。

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

型号:

class Post < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, use: :slugged
  acts_as_ordered_taggable_on :tags

  belongs_to :user

  validates :title, :header, :content, presence: true
  validates :title, uniqueness: true
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, input_html: { multiple: true, class: 'tags' }
  = f.submit 'Save', class: 'btn btn-warning'
= link_to _('Back'), :back

application.js:

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

1 个答案:

答案 0 :(得分:2)

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

在控制器中:

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