ruby on rails - Nested forms not working -
i have 2 models (bcase , c_entry) , trying 10 blank c_entry fields on each bcase creation form!
what did 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 |f| %> <div class="form-inputs"> <% f.simple_fields_for :c_entries |entry| %> <%= entry.input :description %> <% end %> </div> <% end %>
but if open view on localhost in browser form empty. tested , know working bcase attributes not nested attributes. if try initialize 1 object instead if 10 @bcase.c_entries.build
nothing changes, still doenst show nested. further tried create c_entries via rails console , worked. used command bcase.first.c_entries.build
aswell.
edit: okay, problem solved view shows 1 entry instead of 10! doing wrong?
edit2:
rake routes
prefix verb uri pattern controller#action protocols /protocols(.:format) protocols#index post /protocols(.:format) protocols#create new_protocol /protocols/new(.:format) protocols#new edit_protocol /protocols/:id/edit(.:format) protocols#edit protocol /protocols/:id(.:format) protocols#show patch /protocols/:id(.:format) protocols#update put /protocols/:id(.:format) protocols#update delete /protocols/:id(.:format) protocols#destroy pimps / pimps#index new_pimp /new(.:format) pimps#new edit_pimp /:id/edit(.:format) pimps#edit pimp /: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 /:pimp_id/onepager/new(.:format) mepagers#new edit_mepager /:pimp_id/onepager/edit(.:format) mepagers#edit mepager /: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 /:pimp_id/bcase/new(.:format) bcases#new edit_bcase /:pimp_id/bcase/edit(.:format) bcases#edit bcase /: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) |f| %> <%= f.error_notification %> <%= simple_fields_for :c_entries |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 @bcase in simple_form_for
nomethoderror in bcases#new showing c:/users/public/documents/sites/improvement/app/views/bcases/_form.html.erb line #1 raised: undefined method `bcases_path' #<#<class:0x53efbc0>:0x4d195d0>
edit3:
did more testing , included after
<p><%= "nr of c-entries = #{@bcase.c_entries.size}" %></p>
what gives me 10
lines
<%= @bcase.c_entries.each |entry| %> <%= entry.description %> <% end %>
and shows me 10 empty entries! there, 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 bcases_path
routes debugging. form_for works @bcase doesnt solve problems have.
edit5:
it somehow works
<%= @bcase.c_entries.each |hallo| %> <%= f.simple_fields_for :c_entries, hallo |fff| %> <%= fff.input :description %> <% end %> <% end %>
probably cause c_entries specified. solution doesnt lean! possible specify somehow in bcase controller?
ah, found it! forgot =
in <%=
. block execute correctly, not added form.
so write
<%= simple_form_for @bcase |f| %> <div class="form-inputs"> <%= f.simple_fields_for :c_entries |entry| %> <%= entry.input :description %> <% end %> </div>
Comments
Post a Comment