だから私は City と ithas_many :places
と itというモデルを持っていaccepts_nested_attributes_for :places
ます。各場所belongs_to :category
。私が持っている都市のフォームをレンダリングすると、次のf.fields_for :places do |place|
ようになります。
<% f.fields_for :places do |place| %>
<%= render "place_fields", :f => place
<% end %>
私の _place_fields.html.erb には以下が含まれています:
<div class="place_header"><%= f.object.category.name %></div>
<div><%= f.label :name %>: <%=f.text_field :name %> </div>
<div><%= f.text_area :description %> </div>
しかし、新しい場所を追加しようとすると問題が発生します。まず、単純な選択フォームを表示して新しい場所のカテゴリを選択し、category_id に基づいて同じパーシャルをレンダリングします。私は同じアクション内でそれを行います:
def add_place
if params[:category_id]
@place = Place.new(:category_id => params[:category_id])
respond_to do |format|
format.html { return nil }
format.js {
#here comes the render
}
end
else
render_message :header => "Choose category", :partial => "category_select", :over => 10
end
end
しかし、私がそれをやろうとすると$("#places_tab").append("<%= escape_javascript(render :partial => "place_fields", :f => @place %>");
、エラーが発生します。
繰り返しますが、その新しい場所のフィールドをレンダリングする必要がありますが、その方法がわかりません。
アップデート
元の City Formbuilder をアクションに渡し、そのビルダーからその場所を正しくレンダリングすることについてアドバイスを受けましたが、それを行う方法がわかりません。