Вложенная форма не отображается на странице - PullRequest
0 голосов
/ 18 февраля 2020

Я пытаюсь создать вложенную форму, но не могу заставить ее работать. Я использую рельсы 5.2, разработайте и злой мастер. Я пытался использовать камень кокона, но без удачи. Проблема в том, что вложенная форма не отображается на странице, это просто пустой раздел на странице.

Это то, что я пробовал до сих пор с установленным камнем Cocoon, и я добавил // = требуется кокон для приложения. 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

Я создал эти отношения

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

Я добавил значения в пользовательские параметры

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å&nbsp;<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

1 Ответ

0 голосов
/ 18 февраля 2020

Вам нужно создать экземпляр объекта, в действии контроллера

@user.services.new

или, если этот частичный элемент используется только в форме new, которая не используется совместно с формой edit, вы можете попробовать это

<%= f.simple_fields_for @user.services.new do |t| %>

Попробуйте!

...