Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有办法在Rails 6中的表单中向collection\u select字段添加新值?_Javascript_Ruby On Rails_User Interface - Fatal编程技术网

Javascript 有没有办法在Rails 6中的表单中向collection\u select字段添加新值?

Javascript 有没有办法在Rails 6中的表单中向collection\u select字段添加新值?,javascript,ruby-on-rails,user-interface,Javascript,Ruby On Rails,User Interface,在我的博客应用程序中,有帖子,每个帖子都可以有标签。当用户在新建或编辑帖子表单上时,他们当前可以选择或取消选择与帖子关联的标签。post和tag模型之间存在多对多关系,我正在使用bootstrap、bootstrap_form和bootstrap select。这一切看起来都很好。问题是,当用户为他们的帖子分配标签时,这些标签当前必须已经存在(否则他们将不得不中止帖子并添加标签…糟糕的用户体验)。我正在尝试设计一种方法,让用户可以选择创建新标签和/或选择现有标签并将其应用于帖子,所有这些标签都位

在我的博客应用程序中,有帖子,每个帖子都可以有标签。当用户在新建或编辑帖子表单上时,他们当前可以选择或取消选择与帖子关联的标签。post和tag模型之间存在多对多关系,我正在使用bootstrap、bootstrap_form和bootstrap select。这一切看起来都很好。问题是,当用户为他们的帖子分配标签时,这些标签当前必须已经存在(否则他们将不得不中止帖子并添加标签…糟糕的用户体验)。我正在尝试设计一种方法,让用户可以选择创建新标签和/或选择现有标签并将其应用于帖子,所有这些标签都位于collection_select字段的同一帖子表单上,所有标签都可以同时使用

显然,我在谷歌上问了错误的问题……这难道不是一个已经解决的共同需求吗

我要求获得有关向其他功能集合选择字段提供“添加新标签”功能的指导。怎么做才是最好的

主计长员额:

class PostsController < ApplicationController
  before_action :set_post, only: %i[edit update interim destroy]


  # GET /posts
  # GET /posts.json
  def index
    if user_signed_in? && current_user.admin_role
      if params[:tag]
        @posts = Post.tagged_with(params[:tag]).all.order('updated_at DESC').page params[:page]
      else
        @posts = Post.all.order('updated_at DESC').page params[:page]
      end
    else
      if params[:tag]
        @posts = Post.tagged_with(params[:tag]).where(published: true).order('updated_at DESC').page params[:page]
      else
        @posts = Post.where(published: true).order('updated_at DESC').page params[:page]
      end
    end

  end


  # GET /posts/new
  def new
    @post = current_user.posts.build
    @categories = Category.pluck(:name, :id)
  end

  # GET /posts/1/edit
  def edit
    @categories = Category.pluck(:name, :id)
    @cat = @post.category_id
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = current_user.posts.create(post_params)

    respond_to do |format|
      if @post.save
        if params[:interim]
          format.html { redirect_to edit_post_path(@post), notice: 'Post was successfully created.' }
          format.json { redirect_to edit_post_path(@post), status: :created, location: @post, notice: 'Post was successfully created.' }
        elsif params[:complete]
          format.html { redirect_to @post, notice: 'Post was successfully created.' }
          format.json { render :show, status: :created, location: @post }
        end
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        if params[:interim]
          format.html { redirect_to edit_post_path(@post), notice: 'Post was successfully updated.' }
          format.json { redirect_to edit_post_path(@post), status: :ok, location: @post, notice: 'Post was successfully updated.' }
        elsif params[:complete]
          format.html { redirect_to @post, notice: 'Post was successfully updated.' }
          format.json { render :show, status: :ok, location: @post }
        end
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private


  def set_post
    @post = Post.find(params[:id])
  end


  def post_params
    params.require(:post).permit(:title, :content, :user_id, :published, :category_id, :tag_list, :tag, { tag_ids: [] }, :tag_ids,
      comment_attributes: [:id, :title, :user_id, :content, :post_id, :parent_id, :ancestry, :commentable, :commentable_id, :commentable_type])
  end
end
class PostsController
无标签控制器(目前不需要)

后模型:

class Post < ApplicationRecord

    has_many :taggings
    has_many :tags, through: :taggings
    has_rich_text :content

    include PgSearch::Model
    multisearchable :against => [:title, :content]

    def self.published(post)
      post.published
    end

    def self.tagged_with(name)
      Tag.find_by!(name: name).posts
    end

    def self.tag_counts
      Tag.select('tags.*, count(taggings.tag_id) as count').joins(:taggings).group('taggings.tag_id')
    end

    def tag_list
      tags.map(&:name).join(', ')
    end

    def tag_list=(names)
      self.tags = names.split(',').map do |n|
        Tag.where(name: n.strip).first_or_create!
      end
    end

end
class Post[:标题,:内容]
def自我发布(post)
后出版
结束
def自我标记为(名称)
Tag.find_by!(姓名:姓名)。职位
结束
def self.tag_计数
Tag.select('tags.*,count(taggings.Tag\u id)as count')。join(:taggings.group('taggings.Tag\u id'))
结束
def标签列表
tags.map(&:name).join(“,”)
结束
def tag_list=(名称)
self.tags=names.split(',').map do | n|
Tag.where(名称:n.strip)。首先\u或\u创建!
结束
结束
结束
标签型号:

class Tag < ApplicationRecord
  has_many :taggings
  has_many :posts, through: :taggings
end
class标记
标记模型:

class Tagging < ApplicationRecord
  belongs_to :tag
  belongs_to :post
end
类标记
Post Form Partial(实际的新建视图和编辑视图只渲染此表单):







目前没有用于标记的表单

可以使用和以相同的形式创建嵌套记录。但是,从用户体验和代码组织的角度来看,我只想使用ajax将请求发送到单独的标记控制器,并动态创建标记。@max感谢您的响应……您已经将我的想法转向JavaScript以寻求解决方案,而到目前为止,我一直在严格遵守rails。您可以使用和。但从用户体验和代码组织的角度来看,我只想使用ajax将请求发送到单独的标记控制器,并动态创建标记。@max感谢您的回复
<%= bootstrap_form_for @post, local: true, html: { class: 'form-horizontal' } do |f| %>

  <%= f.text_field :title %>

  <%= f.rich_text_area :content, control_class: 'trix-content-edit' %>

  <%= f.collection_select :category_id, Category.all, :id, :name %>

  <%= f.form_group :published, label: { text: "Publication Status" } do %>
    <%= f.radio_button :published, true, label: "Published" %>
    <%= f.radio_button :published, false, label: "Draft" %>
  <% end %>

  <%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {label: 'Tags', include_blank: true}, {class: 'selectpicker show-tick', multiple: 'multiple', title: 'Make your selection...', 'data-live-search': 'true', 'data-actions-box': 'true'} %>

  <br><br>
  <%= f.submit "Save and Resume Editing", name: "interim", class: "btn btn-primary" %>
  <%= f.submit "Save and Quit", name: "complete", class: "btn btn-primary" %>
  <br><br>

<% end %>