I have two models (Bcase and C_entry) and I am trying to get 10 blank c_entry fields on each Bcase creation form!
What I did is following:
Models:
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
form (gets rendered in bcase -> 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 %>
But if I open the view on localhost in my browser the form is empty. I tested everything and I know that the everything is working for the bcase attributes but not for the nested attributes. If I try to initialize only 1 object instead if 10 with @bcase.c_entries.build nothing changes, still doenst show anything nested. Further I tried to create c_entries via rails console and that worked. I used the command Bcase.first.c_entries.build aswell.
EDIT: Okay, the problem is solved but now my view shows only 1 entry instead of 10! What am I doing wrong?
EDIT2:
Rake routes
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
Error with @bcase in simple_form_for
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:
Did some more testing and included after
<p><%= "Nr of c-entries = #{@bcase.c_entries.size}" %></p>
What gives me 10 back the lines
<%= @bcase.c_entries.each do |entry| %>
<%= entry.description %>
<% end %>
And that shows me 10 empty entries! So they are there, I just cant see them somehow!
[#<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:
Included now bcases_path into my routes for debugging. Now my form_for works with @bcase but that doesnt solve the problems I have.
EDIT5:
It somehow works with
<%= @bcase.c_entries.each do |hallo| %>
<%= f.simple_fields_for :c_entries, hallo do |fff| %>
<%= fff.input :description %>
<% end %>
<% end %>
Probably cause the c_entries are specified. But this solution doesnt look so lean! Is it possible to specify this somehow in the bcase controller?
desribtionwith ab?