I'm trying to build a nested form, but I can't get it to work. I'm using rails 5.2, devise and wicked wizard. I have tried to use the cocoon gem, but without luck. The problem is, that the nested form doesn't show up on the page, it's just a blank section on the page.
This is what I have tried so far with the cocoon gem installed, and I have added //= require cocoon to application.js.
I have a user table and a services table. The service table is the nested form.
This is how I created the services table
class Services < ActiveRecord::Migration[5.2]
def change
create_table :services do |t|
t.string :name
t.text :description
t.integer :duration
t.integer :price
t.timestamps
end
end
end
I have added a foreign key to the services table like this
AddUserReferenceToServices
class AddUserReferenceToServices < ActiveRecord::Migration[5.2]
def change
add_reference :services, :user, foreign_key: true
end
end
I have created these relations
user.rb
class User < ApplicationRecord
has_many :services
accepts_nested_attributes_for :services, reject_if: :all_blank, allow_destroy: true
end
service.rb
class Service < ApplicationRecord
belongs_to :user
end
I have added values to the user params
def user_params
params.require(:user).permit(clinic_images: [], profession_ids: [], speciality_ids: [], services_attributes: [:id, :description, :name, :duration, :price, :_destroy])
end
main_form.html.erb
<div class="content">
<%= simple_form_for @user, url: wizard_path, method: :put do |f| %>
<div class="specializations-section">
<h2 class="page-title">Professioner</h2>
<div class="field select-field ">
<%= f.association :professions, input_html: { id: 'professions-select', class: 'js-example-basic-multiple' } %>
</div>
<div class="content">
<p>Mangler der en profession på listen? Så skriv til os på <a href="mailto:info@betterwing.dk?Subject=Forslag%20til%20ny%20profession">info@betterwing.dk</a></p>
</div>
</div>
<div id="services">
<%= f.simple_fields_for :services do |t| %>
<%= render 'services/services_fields', :f => t %>
<% end %>
<div class="links">
<%= link_to_add_association 'add services', f, :services, :partial => 'services/services_fields' %>
</div>
</div>
services/_services_fields.html.erb
<%= f.input_field :name, required: true, autofocus: true, autocomplete: "Navn på behandling", placeholder: "Navn på behandling" %>
<%= f.input_field :description, required: true, rows: '1', autofocus: true, autocomplete: "Beskrivelse af behandling", placeholder: "Beskrivelse af behandling"%>
<%= f.input_field :duration, required: true, rows: '1', autofocus: true, autocomplete: "Tid", placeholder: "Tid"%>
<%= f.input_field :price, required: true, rows: '1', autofocus: true, autocomplete: "Pris", placeholder: "Pris"%>
<%= link_to_remove_association 'x', f %>
registration_steps_controller.rb
class RegistrationStepsController < ApplicationController
include Wicked::Wizard
steps :company_general, :company_images, :practitioners_general, :practitioners_professions, :practitioners_educations
def new
@user.services.new
end
def show
@user = current_user
render_wizard
end
def update
@user = current_user
# Change @user.attributes(user_params) by @user.update_attributes(user_params)
@user.update_attributes(user_params)
render_wizard @user
end
private
def user_params
params.require(:user).permit(clinic_images: [], profession_ids: [], speciality_ids: [], services_attributes: [:id, :description, :name, :duration, :price, :_destroy])
end
end