我正忙着开发票应用程序而且我试图将cocoon gem中的嵌套表单放在<tbody></tbody>
中。嵌套表单工作正常,但它不会出现在<tbody></tbody>
中,而是出现在桌头上方的某个随机位置。我认为这是因为你不能在桌子内部<div class=nested-fields></div>
,但我不确定如何以不同的方式做到这一点。
我的发票/ _form.html.erb中有这个:
<%= form_for @invoice do |f| %>
<% if @invoice.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@invoice.errors.count, "error") %> prohibited this invoice from being saved:</h2>
<ul>
<% @invoice.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="col-sm-12">
<table class="table table-striped">
<thead>
<tr>
<th class="hidden-480"> Hoeveelheid </th>
<th class="hidden-480"> Beschrijving </th>
<th class="hidden-480"> Bedrag </th>
<th class="hidden-480"> Totaal </th>
<th class="hidden-480"> Btw(%) </th>
</tr>
</thead>
<tbody>
<%= f.fields_for :products do |product| %>
<%= render 'product_fields', f: product %>
<%= link_to_add_association 'Item toevoegen', f, :products, class: 'btn btn-primary btn-success' %>
<% end %>
</tbody>
</table>
</div>
</div>
<% end %>
发票/ _product_fields.html.erb
<div class="nested-fields">
<tr>
<td> <%= f.text_field :quantity %> </td>
<td> <%= f.text_area :description %> </td>
<td> <%= f.number_field :unitprice %> </td>
<td> €200 </td>
<td> <%= f.select(:btw, [[' 21%', 21, title: '21%'],[' 6%', 6, title: '6%'], [' 0%', 0, title: '0%']]) %> </td>
</tr>
<%= link_to_remove_association 'x', f, class: 'btn btn-primary btn-danger' %>
</div>
Invoice.rb - 型号
class Invoice < ActiveRecord::Base
has_one :company
has_one :customer
has_many :products
accepts_nested_attributes_for :customer, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :products, reject_if: :all_blank, allow_destroy: true
validates :number, :currency, :date, :duedate, :btwtotal, :subtotal, :total, presence: true
end
Product.rb - model
class Product < ActiveRecord::Base
belongs_to :invoice
end
Invoices_controller.rb
class InvoicesController < ApplicationController
before_action :set_invoice, only: [:show, :edit, :update, :destroy]
# GET /invoices
# GET /invoices.json
def index
@invoices = Invoice.all
end
# GET /invoices/1
# GET /invoices/1.json
def show
end
# GET /invoices/new
def new
@invoice = Invoice.new
@invoice.products.build
end
# GET /invoices/1/edit
def edit
end
# POST /invoices
# POST /invoices.json
def create
@invoice = Invoice.new(invoice_params)
respond_to do |format|
if @invoice.save
format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
format.json { render :show, status: :created, location: @invoice }
else
format.html { render :new }
format.json { render json: @invoice.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /invoices/1
# PATCH/PUT /invoices/1.json
def update
respond_to do |format|
if @invoice.update(invoice_params)
format.html { redirect_to @invoice, notice: 'Invoice was successfully updated.' }
format.json { render :show, status: :ok, location: @invoice }
else
format.html { render :edit }
format.json { render json: @invoice.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invoices/1
# DELETE /invoices/1.json
def destroy
@invoice.destroy
respond_to do |format|
format.html { redirect_to invoices_url, notice: 'Invoice was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invoice
@invoice = Invoice.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:number, :currency, :date, :duedate, :btwtotal,
:subtotal, :total, :footer, customers_attributes: [:id, :company_name, :address_line_1, :zip_code, :_destroy],
companies_attributes: [:id, :btw_number, :iban_number, :kvk_number, :company_name, :_destroy],
products_attributes: [:id, :quantity, :description, :unitprice, :btw, :total])
end
end
知道发生了什么事吗?非常感谢帮助!
答案 0 :(得分:1)
你可以简单地使用cocoon gem提供的内置方法来实现link_to_add_association,
参考: https://github.com/nathanvda/cocoon#link_to_add_association
{{1}}
P.S这是我在stackoverflow中的第一个答案,所以如果我不够清楚,我会提前道歉。
答案 1 :(得分:0)
您可以参考cocoon gem的控制插入行为部分,以获取有关插入嵌套字段的更多参考。
例如,
File used to load the logback properties: day3/logback/logback.properties
Console logging pattern used: %date{yyyy-MMM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} %msg%n
Console appender started...
File logging pattern used: %date{yyyy-MMM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} %msg%n
Directory already available: D:\EclipseProjects\springClasses\SpringClasses\target\log
File rolling pattern used: application.%d{yyyy-MMM-dd_HH:mm:ss}_%i.log
23:23:32,041 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@915416632 - No compression will be used
23:23:32,045 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@915416632 - Will use the pattern D:/EclipseProjects/springClasses/SpringClasses/target/log/application.%d{yyyy-MMM-dd_HH:mm:ss}_%i.log for the active file
23:23:32,047 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f0eb4b4 - The date pattern is 'yyyy-MMM-dd_HH:mm:ss' from file name pattern 'D:/EclipseProjects/springClasses/SpringClasses/target/log/application.%d{yyyy-MMM-dd_HH:mm:ss}_%i.log'.
23:23:32,047 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f0eb4b4 - Roll-over every second.
23:23:32,047 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f0eb4b4 - Setting initial period to Sat Aug 20 22:09:46 IST 2016
23:23:32,050 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f0eb4b4 - The date pattern is 'yyyy-MMM-dd_HH:mm:ss' from file name pattern 'D:/EclipseProjects/springClasses/SpringClasses/target/log/application.%d{yyyy-MMM-dd_HH:mm:ss}_%i.log'.
23:23:32,050 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f0eb4b4 - Roll-over every second.
23:23:32,050 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7f0eb4b4 - Setting initial period to Sat Aug 20 22:09:46 IST 2016
23:23:32,052 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ApplicationRollingFileAppender] - Active log file name: D:/EclipseProjects/springClasses/SpringClasses/target/log/application.log
23:23:32,052 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ApplicationRollingFileAppender] - File property is set to [D:/EclipseProjects/springClasses/SpringClasses/target/log/application.log]
Logger name: ROOT, Log Level: DEBUG
Logger name: com, Log Level: null
Logger name: com.rsa, Log Level: null
Logger name: com.rsa.springclasses, Log Level: null
Logger name: com.rsa.springclasses.day3, Log Level: null
Logger name: com.rsa.springclasses.day3.logback, Log Level: DEBUG
Logger name: com.rsa.springclasses.day3.logback.AppConfigJunitTest, Log Level: TRACE
Logger name: org, Log Level: null
Logger name: org.hibernate, Log Level: ERROR
Logger name: org.hibernate.type, Log Level: ERROR
23:23:32,057 |-INFO in c.q.l.co.rolling.helper.RenameUtil - Renaming file [D:\EclipseProjects\springClasses\SpringClasses\target\log\application.log] to [D:\EclipseProjects\springClasses\SpringClasses\target\log\application.2016-Aug-20_22:09:46_0.log]
23:23:32,080 |-WARN in c.q.l.co.rolling.helper.RenameUtil - Failed to rename file [D:\EclipseProjects\springClasses\SpringClasses\target\log\application.log] as [D:\EclipseProjects\springClasses\SpringClasses\target\log\application.2016-Aug-20_22:09:46_0.log].
23:23:32,162 |-WARN in c.q.l.co.rolling.helper.RenameUtil - Please consider leaving the [file] option of RollingFileAppender empty.
23:23:32,162 |-WARN in c.q.l.co.rolling.helper.RenameUtil - See also http://logback.qos.ch/codes.html#renamingError
23:23:32,214 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - first clean up after appender initialization
23:23:32,215 |-INFO in c.q.l.core.rolling.helper.TimeBasedArchiveRemover - Multiple periods, i.e. 336 periods, seem to have elapsed. This is expected at application start.
2016-Aug-20_23:23:32.057 [main] DEBUG ROOT Logger context created.
Appenders available: ApplicationRollingFileAppender
Appenders available: ApplicationConsoleAppender
2016-Aug-20_23:23:32.434 [main] DEBUG c.r.s.d.logback.AppConfigJunitTest Debug message
2016-Aug-20_23:23:32.435 [main] ERROR c.r.s.d.logback.AppConfigJunitTest Error message
2016-Aug-20_23:23:32.435 [main] INFO c.r.s.d.logback.AppConfigJunitTest Info message
2016-Aug-20_23:23:32.436 [main] TRACE c.r.s.d.logback.AppConfigJunitTest Trace message
2016-Aug-20_23:23:32.436 [main] WARN c.r.s.d.logback.AppConfigJunitTest Warn message