Answer the question
In order to leave comments, you need to log in
Generating (render) form editing nested object via ajax (with jquery-fileupload-rails)?
I have 2 models:
has_many :building_views
belongs_to :building
image
) and title ( title
). jquery-fileupload-rails
(creating a BuildingView for each image)# other defs ...
def edit
@building = Building.find(params[:id])
end
def update
@building = Building.find(params[:id])
if @building.update_attributes(building_params)
flash[:success] = 'Woohoo!'
redirect_to admin_buildings_url
else
render :edit
end
end
# other defs ...
private
def building_params
params.require(:building).permit(:all_building_params,
building_views_attributes: [
:_destroy,
:id,
:building_id,
:image,
:title
])
end
%h1 Edit building
= form_for [:admin, @building] do |f|
= render 'admin/buildings/form', f: f
.form-element-container
.form-element.b-form-labels
.form-element.b-form-fields
= f.submit 'Edit building'
= link_to t('admin.cancel'), :back
= form_for [:admin, BuildingView.new] do |f_bv|
= f_bv.hidden_field :building_id, value: @building.id
= f_bv.file_field :image, multiple: true, name: 'building_view[image]'
// fields for Building...
#building_views
.form-element-container
.form-element
%h2 Building views
= f.fields_for :building_views do |bv_form|
= render 'admin/buildings/form_building_views', f: bv_form
.form-element-container
.form-element.b-form-labels Image:
.form-element.b-form-fields= image_tag(f.object.image_url(:thumb)) if f.object.image
.form-element-container
.form-element.b-form-labels Title:
.form-element.b-form-fields= f.text_field :title
jQuery ->
$('#building_view_image').fileupload(
dataType: 'script'
)
class Admin::BuildingViewsController < ApplicationController
def create
@building_view = BuildingView.create(building_view_params)
end
private
def building_view_params
params.require(:building_view).permit(:building_id, :image)
end
end
- if @building_view.new_record?
== alert('Oops!');
- else
== $('#building_views').append('#{j render('admin/buildings/form_building_views')}');
ActionView::Template::Error (undefined local variable or method `f' for #<#<Class:0x007ffaa52faad8>:0x007ffaa7393970>):
f
:j render( partial: 'admin/buildings/form_building_views', locals: { f: ??? } )
Answer the question
In order to leave comments, you need to log in
In general, without understanding how I can f
transfer this to this partial (as far as I can judge it and not transfer it), I decided to go by recreating this partial “by hand”.
Firstly, I created a separate partial for ajax insertion (I immediately give the result, below I explain where the variable @building
and helper appeared in it building_nested_model_count
):
.form-element-container
.form-element.b-form-labels= image_tag(@building_view.image_url(:thumb)) if @building_view
.form-element.b-form-fields= text_field_tag "building[building_views_attributes][#{building_nested_model_count(@building_view, @building)}][title]", nil, placeholder: 'Title'
= hidden_field_tag "building[building_views_attributes][#{building_nested_model_count(@building_view, @building)}][id]", @building_view.id
@building
class Admin::BuildingViewsController < ApplicationController
def create
@building_view = BuildingView.create(building_view_params)
@building = @building_view.building # определяем @building
end
private
def building_view_params
params.require(:building_view).permit(:building_id, :image)
end
end
params
in the fields id
and name
forms for each BuildingView (see the ajax partial), let's calculate it using the helper: module Admin::BuildingsHelper
def building_nested_model_count(nested_var, building)
nested_class = nested_var.class
nested_class.where(building_id: building.id).count
end
end
- if @building_view.new_record?
== alert('Oops!');
- else
== $('#building_views').append('#{j render('admin/buildings/form_building_views_ajax')}');
title
saved.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question