पहले से ही समस्या को हल करने का प्रयास करने वाला एक महीना बहुत जटिल नहीं है: 3 मॉडल हैं - टीम, उपयोगकर्ता और टीम_यूसर (has_namy: through) गतिशील रूप से जोड़ने की क्षमता करने के लिए संपादन और नई टीम के रूप में इस टीम के सदस्य।रेल 5 - संपादन फ़ॉर्म में नेस्टेड फ़ील्ड को गतिशील रूप से कैसे जोड़ें?
परिदृश्य:
- एक उपयोगकर्ता टीम नए रूप
- की बात आती है टीम का नाम इंगित करता है
- नाम क्षेत्र का चयन करें उपयोगकर्ता (टीम के सदस्य) के बाद
- सदस्य जोड़ें बटन पर क्लिक
- किसी सदस्य के सत्यापन के बाद टेक्स्ट फ़ील्ड में टीम के नाम फ़ील्ड के बाद जोड़ा जाता है +
- के विपरीत बटन हटाएं चयनकर्ता से हटाएं उसके (सदस्य) का नाम (के रूप में यह पहले से ही एक टीम के सदस्य है)
- selite में अगले उपयोगकर्ता का चयन करता है, और नई टीम और टीम के सदस्यों को बचाने के लिए सदस्य जोड़ें बटन
- प्रेस सबमिट बटन पर क्लिक करें
कठिनाइयाँ:
- मैं मणि कोकून के माध्यम से करने की कोशिश की है, लेकिन यह उपयोगकर्ता का चयन करने के लिए विभिन्न parshaly बनाने के लिए असंभव है यह (चयन) में शामिल होना और सदस्यों जोड़ा गया है (पूरा नाम - पाठ)
- यदि के माध्यम से किया जाता है <% = from_for ... रिमोट: सही%> और नियंत्रक टीम_कंट्रोलर में एक अलग नियंत्रक या नई कार्रवाई, यह अपने दूसरे सबमिट बटन के साथ नेस्टेड (आकार और फॉर्म टीम team_user) के दो रूप होंगे। अनुलग्नक रूप, जैसा कि मैं समझता हूं, गड़बड़ नहीं है। रूप में
- परिवर्तन (टीम के नाम बदल रहा है और जोड़ने/केवल मूल रूप से प्रपत्र टीम प्रस्तुत करने के लिए बचाने के लिए पर क्लिक करने के बाद टीम के सदस्यों को दूर गिनती है)
एप्लिकेशन/मॉडल/user.rb
class User < ApplicationRecord
has_many :team_users
has_many :teams, through: :team_users
accepts_nested_attributes_for :team_users, :teams, allow_destroy: true
end
एप्लिकेशन/मॉडल/team.rb
class Team < ApplicationRecord
has_many :team_users
has_many :users, through: :team_users
accepts_nested_attributes_for :team_users, allow_destroy: true, reject_if: proc { |a| a['user_id'].blank? }
end
एप्लिकेशन/मॉडल/team_user.rb
class TeamUser < ApplicationRecord
belongs_to :team
belongs_to :user
accepts_nested_attributes_for :team, :user, allow_destroy: true
end
एप्लिकेशन/नियंत्रक/teams_controller.rb
class TeamsController < ApplicationController
before_action :set_team, :set_team_users, only: [:show, :edit, :update, :destroy]
before_action :set_team_ancestry, only: [:new, :edit, :create, :update, :destroy]
before_action :set_new_team_user, only: [:new, :edit]
before_action :logged_in_user
layout 'sidebar'
# GET /teams
def index
@teams = Team.search(params[:search], :name).sorting(params[:sort], params[:direction]).paginate(page: params[:page])
end
# GET /teams/1
def show
end
# GET /teams/new
def new
@team = Team.new(parent_id: params[:parent_id])
end
# GET /teams/1/edit
def edit
@team_users = @team.team_users
end
# POST /teams
def create
@team = Team.new(team_params)
respond_to do |format|
if @team.save
format.html { redirect_to @team, success: t('.flash.success.message') }
else
format.html { render :new, danger: t('.flash.danger.message') }
end
end
end
# PATCH/PUT /teams/1
def update
respond_to do |format|
if @team.update(team_params)
format.html { redirect_to @team, success: t('.flash.success.message') }
else
format.html { render :edit, danger: t('.flash.danger.message') }
end
end
end
# DELETE /teams/1
def destroy
@team.destroy
respond_to do |format|
format.html { redirect_to teams_url, success: t('.flash.success.message') }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_team
@team = Team.find(params[:id])
end
def set_team_ancestry
@team_collection = Team.where.not(id: params[:id]).all.each { |c| c.ancestry = c.ancestry.to_s + (c.ancestry != nil ? "/" : '') + c.id.to_s
}.sort{ |x,y| x.ancestry <=> y.ancestry }.map{ |c| ["-" * (c.depth - 1) + c.name,c.id] }
end
def set_team_users
@team_users_collection = User.all.collect { |p| [ p.name, p.id ] }
end
def set_new_team_user
@team_users_new = @team.team_users.build
end
# Never trust parameters from the scary internet, only allow the white list through.
def team_params
params.require(:team).permit(
:name,
:parent_id,
team_users_attributes: [:_destroy, :id, :user_id]
)
end
end
क्या आपको कोई समाधान मिला? – Thrasher
@ थ्रेसर अभी तक नहीं ( –
यह वही उत्तर है जिसे मैं ढूंढ रहा हूं। उम्मीद है कि कोई जल्द ही मदद करेगा! – pappy