Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

ruby - Rails: Filterable nested form checkboxes

I am currently building a label feature, similar to GitHub's label feature, where user's can filter out the label and select them as they wish. enter image description here

As you can see, there is a search field that when searched for a label should send a new request to a micro controller that only renders the labels list instead of sending the entire page layout.

Section model

has_many :taggables, dependent: :destroy
has_many :labels, through: :taggables

Label model

has_many :taggables, dependent: :destroy
has_many :sections, through: :taggables

Taggable model

belongs_to :section
belongs_to :label

And in my view that corresponds to the searchable label, I have this code, which works fine for nested fields as well, but the problem arises when implementing the search feature.

<%= form_with model: some_model do |form|
  <%= form.fields_for :sections, Section.new, child_index: "NEW_RECORD" do |section| %>
    <%= section.collection_check_boxes :label_ids, labels, :id, :name do |b| %>
      <%= b.check_box %>
      # some more code
    <% end %>
  <% end %>
<% end %>

Basically, I want a micro-controller that handles the search and only sends in the result like this.

class App::Labels::SectionFilterLabelsController < App::BaseController
  layout false

  include Labels::LabelList

  def index
    @project = Project.find(params[:project_id])
    skip_policy_scope

    @labels  = project_labels
  end
end
# app/labels/section_filter_labels/index.html.erb

<%= form.collection_check_boxes :label_ids, @labels, :id, :name do |b| %>
  <%= b.check_box %>
  # some more code
<% end %>

Now this view should only render the checkbox, without the entire layout, but the form instance is not defined in this view as expected.

How do I send/define the form instance? I have tried using the

<%= collection_check_boxes "post[sections_attributes][0]", :label_ids, labels, :id, :name %>
# some code

This solves the missing form instance, but in reality, it does not work, because the [0] attribute should be dynamically rendered and should not be static.

I am scratching my head for the past 3 days. Any help is appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...