チェーンされたoptions_for_selectでnested_form_fields gemを使用しています。 「選択されたオプション」は保存されません。連鎖自体は機能しています。宝石はjavascriptで書かれているので、gemとjQueryチェーンの間に基本的な矛盾があるかもしれません。うまくいけば、私は別のものを見下ろしている。nested_form_fields gem、jQueryチェーンが動作していない場合のoptions_for_select
モデル - user.rb:
has_many :state_registrations, inverse_of: :user,
dependent: :destroy
accepts_nested_attributes_for :state_registrations
モデル - state_registrations.rb:
belongs_to :user, inverse_of: :state_registrations
ビュー - ユーザー/ _form.html.erb:
<div class="col-sm-12 col-md-12 form-group">
<%= f.nested_fields_for :state_registrations, :html => { :multipart => true },
wrapper_tag: :div do |state_registration_fields| %>
<div class="row" >
<div class="col-sm-1 col-md-1"></div>
<div class="col-sm-3 col-md-3">
<%= state_registration_fields.label :state_license, "State" %>
<%= state_registration_fields.select :state_license,
options_for_select(@state_licenses_1), {}, {class:
"state_license form-control btn-default btn-block",
name: "state_license"} %>
</div>
....
<div class="col-sm-3 col-md-3">
<%= state_registration_fields.label :state_license_number,
"License Number or 'Exempt' " %>
<%= state_registration_fields.text_field :state_license_number,
class: "form-control input", type: "text", placeholder: "License
Number" %>
</div>
....
Javascriptを - custon .js: これは、Mika Tuupola Chained selects plugin for jQuery and Zeptoの優れた作品に続いています。
$(document).ready(function() {
$(".state_license_name_user").remoteChained({
parents : ".state_license",
url : "state_registrations/state_license_name_user",
loading : "Loading...",
clear : true
});
});
コントローラ - state_registrations_controller.rb: これが連鎖しているものである:
def state_license
@state_licenses = ['State (select)'] + StateRegistration.all.order(:state_license).pluck(:state_license).uniq
end
def state_license_name_user
state_license_name_users = StateRegistration
.where("state_license = ?", params[:state_license])
.order(:state_license_name_user)
.pluck(:state_license_name_user).uniq
render :json => state_license_name_users.map {|value| [value, value]}
end
コントローラ - users_controller.rb: - どのISN state_license_number:
def update
@user = User.includes(:state_registrations).find(params[:id])
raise params.inspect
paramsがショーを返さチェーンされていない - state_registrationsハッシュの一部です。連鎖options_for_select:state_licenseと:
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"xxxx==",
"user"=>{"email"=>"xxx", ..., "state_registrations_attributes"=>{"0"=
{"state_license_number"=>"OREGON-12345", "submitted_by"=>"user"}}, ...,
"state_license"=>"Oregon", "state_license_name_user"=>"Tax Preparer",
"commit"=>"UPDATE Profile", "controller"=>"users", "action"=>"update",
"id"=>"1"}
3つのフィールド:state_license:state_license_name_userは、(上記の図に示されていない)、(終わりに)分離されstate_license_name_user、そして:state_license_numberの一部でありますstate_registrationsテーブル。それらはusersテーブルに個別に含まれていません。 state_registrationおよびuserモデルが索引付けされる。すなわち、state_registrations表はuser_idフィールドを持ち、users表はstate_registration_idフィールドを持つ。
state_licenseとstate_license_name_userは、collection_selectを使用し、各レコードの子インデックスをネームスペースに使用しています(ただし、私は正しく実行しているかどうかはわかりません)。私は連鎖について心配する前にこの節約をする必要があると思います。 (1)レコードはレコードが1つしかない場合に機能します。 (2)複数のレコードで壊れます。
nested_form_fields gemは 'Namespaced Associations'を提供します。しかし、私は示唆された使用法を理解していません。 Nested_Form_Fields documentation
gemはjavascriptで書かれているため、jQueryチェーンでoptions_for_selectを使用すると、gemのコードを書き直さなければ実現できない場合があります。しかし、私はいくつかのフィードバックを得たいと思います。前もって感謝します。
あなたのビューを見ると、state_license_numberはf.nested_fields_forの中にネストされていないようです:state_registrations。しかし、それがstate_registrationsのフィールドである必要があります。もちろん、jqueryアドオンなしでこの作業を行う必要があります。名前空間の関連付けを使用しないので、ドキュメントのその部分を無視できます。 – Nico
@nico Nico、ありがとうございます。ジャスティン・ラブと私は今日の午後早く解決し、後で答えを発表する予定です。この問題は、あなたの宝石とは何の関係もなく、選択されたjqueryチェーンのオプションとの競合もありませんでした。むしろ、私は国家登録のための協会を通じて多くのものを確立する必要があります。私は誤ってマスタテーブルに保存しようとしていました。 – user3763682
ああ、あなたがそれを解決して聞いてよかった! – Nico