0

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?

2
  • It looks good. Sure it is not a typo, you write desribtion with a b? Commented Jun 26, 2014 at 9:00
  • Oh, well noticed sir. I changed the column name from describtion to description lately and forgot to change it in the forms and controller! I corrected that now but it still doenst work. I cant explain myself whats missing. Looked into various guides and its not even much code so the space for mistakes it so small... Commented Jun 26, 2014 at 9:03

1 Answer 1

1

Ah, found it! You forgot the = in the <%=. So the block will execute correctly, but will not be added to the form.

So write

<%= simple_form_for @bcase do |f| %>
  <div class="form-inputs">
    <%= f.simple_fields_for :c_entries do |entry| %> 
      <%= entry.input :description %>
    <% end %>
  </div>
Sign up to request clarification or add additional context in comments.

9 Comments

Okay, but now I've got the problem that its only showing 1 entry and not all 10!
In your first comment you said it was working perfectly, so I guess it was not :) Instead of writing :bcase, you should probably refer to the instance variable. So I updated my answer. For debugging you could include something like <p><%= "Nr of c-entries = #{@bcase.c_entries.count}" %></p>.
Yea, sorry I was so overwelmed that I saw the form fields :D I totally forgot to check if its working for multiple entries. And its not :( Since my bcase model is nested into another model I changed the <%= simple_form_for @bcase do |f| %> to <%= simple_form_for @bcase(@pimp) do |f| %> but that didnt change anything. The debug function that you provided wont work, because the c_entries are not saved at that state. Is there a way to make it work to count non saved entries? Big thanks!
Use size instead of count (i mix them up). If you write just @bcase, does it work? If you need a nested route, you should write simple_form_for [@pimp, @bcase] (or reverse them, do not know the order by heart). I would expect the @bcase(@pimp) to throw an error, that is not even ruby :)
Ok buddy. It's not working with simple_form_for [@pimp, @bcase] because my routes are a little customized. But bcase_path(@pimp) should do it I guess (but I might be wrong ;)). The debug method shows the right amount of objects (10). So it has to be the view, right?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.