【问题标题】:Nested form doesn't show up on page嵌套表单未显示在页面上
【发布时间】:2020-02-18 06:21:01
【问题描述】:

我正在尝试构建一个嵌套表单,但我无法让它工作。我正在使用 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

我已经创建了这些关系

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

ma​​in_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)创建一个空服务来填写(实际上不需要,除非你真的想要——直到用户按下按钮添加新服务?)和 simple_fields_for 为建议是完全错误的。所以不确定这如何回答任何问题?但我也没有看到真正的错误,除了 1)缺少嵌套元素的周围 div 和 2)没有关闭 div。
  • 是的,它现在已经解决了,它正在使用
  • @nathanvda 如果你仔细看这个问题,它会说nested_form 没有出现在页面上,所以很明显,OP 期望一个文件字段在加载时出现在表单中,那就是为什么我们在控制器中初始化一个空对象以在页面加载时在表单中显示至少一个嵌套的表单对象。当然,您可以在add 按钮的帮助下添加更多内容。所以,先生,最好在得出结论之前正确地检查问题细节。
  • 也许@RajdeepSingh 应该更准确地阅读我的评论。我说不需要更改控制器(但没有错)。但是simple_fields_for @user.services.new 是错误的,因为它永远不会遍历已保存的services 集合。和 OP:添加括号实际上并没有改变任何东西,所以你一定改变了其他东西。但很高兴你把它修好了!
  • @nathanvda 我刚刚改写了该行以使其不那么混乱。但是,当第一种方法不适用于 OP 时,我建议使用第二种方法。您可以在回答 cmets 部分看到对话,对于 OP 似乎没有任何效果,所以只是试图帮助他。

标签: ruby-on-rails devise nested-forms cocoon-gem wicked-gem


【解决方案1】:

您需要在控制器操作中实例化对象

@user.services.new

或者如果这个部分只用在new表单中,不与edit表单共享,你可以试试这个

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

试一试!

【讨论】:

  • 你在哪里设置@user控制器?
  • 我认为它在我的registration_steps_controller 中。我已将控制器添加到我的问题中,还添加了@user.services.new,但表单仍然没有显示。
  • 我的意思是你要加载 new.html.erb 吗?
  • 啊抱歉 :-) 不,我正在尝试加载 services/_services_fields.html.erb
  • 我不知道为什么,但是当我更改时我会工作 到这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
  • 2017-06-21
  • 2012-07-08
  • 1970-01-01
相关资源
最近更新 更多