Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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
Ruby on rails 多态模型的集合\u选择_Ruby On Rails_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 多态模型的集合\u选择

Ruby on rails 多态模型的集合\u选择,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我在我的Rails 5.1.3应用程序中将标记设置为多态模型。我正在尝试设置一个集合\u selectup,以便从下拉列表中选择标签 表单创建了新的标记,关联效果很好,但是,我无法将标记名传递到表单中,因此我的标记将name保存为空字符串 \u form.html.erb <%= form_for [taggable, Tag.new] do |f| %> <%= f.collection_select :taggable_id, Tag.order(:name), :id

我在我的Rails 5.1.3应用程序中将
标记设置为多态模型。我正在尝试设置一个
集合\u select
up,以便从下拉列表中选择标签

表单创建了新的标记,关联效果很好,但是,我无法将标记名传递到表单中,因此我的标记将
name
保存为空字符串

\u form.html.erb

<%= form_for [taggable, Tag.new] do |f| %>
  <%= f.collection_select :taggable_id, Tag.order(:name), :id, :name %>
  <%= f.submit %>
<% end %>
结束

标记参数:

=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"z2PLrvETqtq742zmr3pghEYYqoGoLv05gLP3OXopLM+blWWw+HmR4AMMDB+5ET3E5YLXeyhMCFMHfdxJNHlkZA==", "tag"=><ActionController::Parameters {"taggable_id"=>"1"} permitted: false>, "commit"=>"Create Tag", "controller"=>"books/tags", "action"=>"create", "book_id"=>"26"} permitted: false>
tag.rb

class TagsController < ApplicationController
before_action :authenticate_user!

def create
  @tag = @taggable.tags.new(tag_params)
  @tag.user_id = current_user.id

  if @tag.save  
    redirect_to book_path(@taggable.book_id), notice: "Tag saved"
  else
    redirect_to root_path, notice: "Sorry, something went wrong"
  end
end

private

  def tag_params
    params.require(:tag).permit(:name)
  end
class Book < ApplicationRecord
  has_many :tags, as: :taggable
end
class Tag < ApplicationRecord
 belongs_to :taggable, polymorphic: true
end

问题是你在处理这个问题时大错特错了

一个标签系统,其中一个标签只能属于一个资源,作为一个分类法,它几乎毫无价值。相反,您希望与联接表建立多对多关联:

# app/models/tag.rb
class Tag < ApplicationRecord
  has_many :taggings
  has_many :taggables, through: :taggings
end

# This join model represents a tag attached to a resource
# app/models/tagging.rb
class Tagging < ApplicationRecord
  belongs_to :tag
  belongs_to :taggable, polymorphic: true
end

# We extract the taggable feature to a concern so that we don't have to repeat it.
# app/models/concerns/taggable.rb
module Taggable
  extends ActiveSupport::Concern

  included do
    has_many :taggings, as: :taggable
    has_many :tags, through: :taggings
  end
end

# app/models/book.rb
class Book < ApplicationRecord
  include Taggable
end

# just an example
class Film < ApplicationRecord
  include Taggable
end
这是可标记(父)资源的正常创建/更新操作的一部分。一次创建一个标记(创建一个连接记录)是可以做到的,但不是很有用

POST/books/:book_id/tags
将用于创建单个标记并建立关联:

<%= form_for([@taggable, @tag]) do |f| %>
  <%= f.label :name do %>
    <%= f.text_input :name %>
  <% end %>
  <%= f.submit %>
<% end %>

但是,通过适当使用Ajax,您可以让用户通过发送
POST/tags
请求来“内联”创建标记,从而提供更好的用户体验。

请在模型中添加模型和字段?@Ankit done.…感谢您的帮助-您完全正确,我的分类错了。还有一件事:如果我希望每个用户都有自己的图书标签,我需要如何修改代码?此时,所有用户都可以看到所有标签,而不仅仅是他们创建的标签。我试图在标签表中添加一个user\u id列,并在
tags\u controller
中设置值,但它似乎不起作用。我有点避免了这一点,因为它已经足够复杂了。如果你想增加所有权-在标记中添加一个用户id列。但我鼓励你特别提出一个新问题
<%= form_for(@book) do |f| %>
  <%= f.text_input :title %>
  <%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, multiple: true %>
  <%= f.submit %>
<% end %>
<%= form_for([@taggable, @tag]) do |f| %>
  <%= f.label :name do %>
    <%= f.text_input :name %>
  <% end %>
  <%= f.submit %>
<% end %>
class TagsController
  before_action :set_taggable

  # POST /books/:book_id/tags
  def create
    @tag = @taggable.tag.new(tag_params)

    if @tag.save
      redirect_to @taggable, success: "New tag created."
    else
      render :new
    end
  end

  def set_taggable
    @taggable = Book.find(params[:book_id])
  end

  def tag_params
    params.require(:tag).permit(:name)
  end
end