我有两个模型(Bcase和C_entry),我试图在每个Bcase创建表单上获得10个空白c_entry字段!
我所做的是:
型号:
bcase.rb
class Bcase < ActiveRecord::Base
has_many :c_entries, :dependent => :destroy
accepts_nested_attributes_for :c_entries
end
c_entry.rb
class CEntry < ActiveRecord::Base
belongs_to :bcase
end
bcase_controller.rb
def new
@bcase = Bcase.new
10.times {@bcase.c_entries.build}
end
def bcase_params
params.require(:bcase).permit(:pimp_id, :comment_text, :status, c_entries_attributes:[:id, :description, :bcase_id])
end
表格(以bcase呈现 - &gt; new.html.erb)
<%= simple_form_for :bcase, url: bcase_path do |f| %>
<div class="form-inputs">
<% f.simple_fields_for :c_entries do |entry| %>
<%= entry.input :description %>
<% end %>
</div>
<% end %>
但是如果我在浏览器中打开localhost上的视图,表单就是空的。我测试了一切,我知道一切都适用于bcase属性,但不适用于嵌套属性。如果我尝试仅初始化1个对象而不是10 @bcase.c_entries.build
没有任何更改,仍然会显示嵌套的任何内容。此外,我尝试通过rails控制台创建c_entries,这是有效的。我还使用了Bcase.first.c_entries.build
命令。
EDIT2:
佣金路线
Prefix Verb URI Pattern Controller#Action
protocols GET /protocols(.:format) protocols#index
POST /protocols(.:format) protocols#create
new_protocol GET /protocols/new(.:format) protocols#new
edit_protocol GET /protocols/:id/edit(.:format) protocols#edit
protocol GET /protocols/:id(.:format) protocols#show
PATCH /protocols/:id(.:format) protocols#update
PUT /protocols/:id(.:format) protocols#update
DELETE /protocols/:id(.:format) protocols#destroy
pimps GET / pimps#index
new_pimp GET /new(.:format) pimps#new
edit_pimp GET /:id/edit(.:format) pimps#edit
pimp GET /:id(/.:format) pimps#show
POST /(.:format) pimps#create
PUT /:id(.:format) pimps#update
PATCH /:id(.:format) pimps#update
DELETE /:id(.:format) pimps#destroy
new_mepager GET /:pimp_id/onepager/new(.:format) mepagers#new
edit_mepager GET /:pimp_id/onepager/edit(.:format) mepagers#edit
mepager GET /:pimp_id/onepager(.:format) mepagers#show
create_mepager POST /:pimp_id/onepager(.:format) mepagers#create
PUT /:pimp_id/onepager(.:format) mepagers#update
PATCH /:pimp_id/onepager(.:format) mepagers#update
DELETE /:pimp_id/onepager(.:format) mepagers#destroy
new_bcase GET /:pimp_id/bcase/new(.:format) bcases#new
edit_bcase GET /:pimp_id/bcase/edit(.:format) bcases#edit
bcase GET /:pimp_id/bcase(.:format) bcases#show
create_bcase POST /:pimp_id/bcase(.:format) bcases#create
PUT /:pimp_id/bcase(.:format) bcases#update
PATCH /:pimp_id/bcase(.:format) bcases#update
DELETE /:pimp_id/bcase(.:format) bcases#destroy
Simple_form_for
<%= simple_form_for bcase_path(@pimp) do |f| %>
<%= f.error_notification %>
<%= simple_fields_for :c_entries do |ff| %>
<div class="form-actions">
<%= ff.input :description, label: false %>
<% end %>
</div>
<% end %>
bcase_controller.rb
def setall
@pimp = Pimp.find(params[:pimp_id])
@bcase = @pimp.bcase
end
simple_form_for
中的@bcase出错NoMethodError in Bcases#new
Showing c:/Users/Public/Documents/Sites/improvement/app/views/bcases/_form.html.erb where line #1 raised:
undefined method `bcases_path' for #<#<Class:0x53efbc0>:0x4d195d0>
EDIT3:
在
之后进行了一些更多的测试和包含<p><%= "Nr of c-entries = #{@bcase.c_entries.size}" %></p>
是什么让我10
支持
<%= @bcase.c_entries.each do |entry| %>
<%= entry.description %>
<% end %>
这显示了10个空条目!所以他们在那里,我不知道怎么看他们!
[#<CEntry id: nil, bcase_id: nil, order_no: 1, description: nil, hours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 2, description: nil, hours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 3, description: nil, hours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 4, description: nil, hours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 5, description: nil, hours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 6, description: nil, hours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 7, description: nil, hours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 8, description: nil, hours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 9, description: nil, hours: nil, nrc: nil, created_at: nil, updated_at: nil>, #<CEntry id: nil, bcase_id: nil, order_no: 10, description: nil, hours: nil, nrc: nil, created_at: nil, updated_at: nil>]
EDIT4:
现在将bcases_path
包含在我的路由中进行调试。现在我的form_for适用于@bcase,但这并没有解决我遇到的问题。
EDIT5:
它以某种方式与
一起使用<%= @bcase.c_entries.each do |hallo| %>
<%= f.simple_fields_for :c_entries, hallo do |fff| %>
<%= fff.input :description %>
<% end %>
<% end %>
可能导致指定了c_entries。但是这个解决方案看起来并不那么精益!是否可以在bcase控制器中以某种方式指定它?
答案 0 :(得分:1)
=
中的<%=
。因此块将正确执行,但不会添加到表单中。
所以写
<%= simple_form_for @bcase do |f| %>
<div class="form-inputs">
<%= f.simple_fields_for :c_entries do |entry| %>
<%= entry.input :description %>
<% end %>
</div>