我正在尝试构建一个嵌套表单,但我无法让它工作。我正在使用 rails 5.2,设计和邪恶的向导。我曾尝试使用茧宝石,但没有运气。问题是,嵌套表单没有显示在页面上,它只是页面上的一个空白部分。
到目前为止,这是我在安装 cocoon gem 的情况下所尝试的,并且我已将 //= require cocoon 添加到 application.js。
我有一个用户表和一个服务表。服务表是嵌套形式。
这就是我创建服务表的方式
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
我已经像这样在服务表中添加了一个外键
AddUserReferenceToServices
class AddUserReferenceToServices < ActiveRecord::Migration[5.2]
def change
add_reference :services, :user, foreign_key: true
end
end
我已经创建了这些关系
用户.rb
class User < ApplicationRecord
has_many :services
accepts_nested_attributes_for :services, reject_if: :all_blank, allow_destroy: true
end
服务.rb
class Service < ApplicationRecord
belongs_to :user
end
我已经为用户参数添加了值
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_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