26

I have created an simple rails project. All worked fine until I tried to add a new model Paintings that belongs_to treatment and an Patient that has_many Paintings through Treatment. So somehow the nested form I created does not show up, I believe it has to do with the controller! Thanks, and greetings from Germany!

Treatments controller:

class TreatmentsController < ApplicationController
  def create
    @patient = Patient.find(params[:patient_id])
    @treatment = @patient.treatments.create(params[:treatment])
    redirect_to patient_path(@patient)
  end

  def destroy
    @patient = Patient.find(params[:patient_id])
    @treatment = @patient.treatments.find(params[:id])
    @treatment.destroy
    redirect_to patient_path(@patient)  
  end
end

And the form for treatments with nested fields_for that doesn't show up:

<%= form_for([@patient, @patient.treatments.build]) do |f| %>
  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content, :cols => "30", :rows => "10" %>
  </div>
  <div class="field">
    <%= f.label :category_id %>
    <%= f.collection_select :category_id, Category.find(:all), :id, :typ %>
  </div>

  <%= f.fields_for :paintings do |ff| %>
    <div class="field">
      <%= ff.label :name, 'Tag:' %>
      <%= ff.text_field :name %>
    </div>
  <% end %>

  <div class="field">
    <%= f.submit nil, :class => 'btn btn-small btn-primary' %>
  </div>
<% end %>

UPDATE:

Show Site:

<% @patient.treatments.each do |treatment| %>
  <tr>
    <td><%= treatment.category.try(:typ) %></td>
    <td><%= treatment.content %></td>
    <td><%= treatment.day %></td>
    <td><div class="arrow"></div></td>
  </tr>
  <tr>
jvperrin
  • 3,368
  • 1
  • 23
  • 33
Em Sta
  • 1,676
  • 9
  • 29
  • 43
  • 3
    try f.fields_for :paintings, Painting.new do |m|. Also, can u post the models code and the controller code in more details? – Prasad Surase Jun 18 '13 at 18:21
  • haha your the best!! I tried it two days and now you made it with only one litlle bite add of code! Please move your comment to a answer so that i can set it to the right answer! – Em Sta Jun 18 '13 at 18:25

5 Answers5

79

Please try

= f.fields_for :paintings, Painting.new do |p|
jvperrin
  • 3,368
  • 1
  • 23
  • 33
Prasad Surase
  • 6,486
  • 6
  • 39
  • 58
  • Sorry but one more thing, how can i show the name of the image up? – Em Sta Jun 18 '13 at 18:30
  • 1
    i really dont have any idea about the model relationships or the fields. can u pls add them? – Prasad Surase Jun 18 '13 at 18:33
  • I mean in the same view i added to my post in the update! SO that ther is another with the name of the image – Em Sta Jun 18 '13 at 18:48
  • u cant display a painting name for a newly built painting object unless u have specified some default value. in the 'form' u cant cause the object fields hold 'nil' value unless u have specified a default value. in the updated view where u have listed the treatments, u can show the names of all the paintings for a particular treatment. – Prasad Surase Jun 18 '13 at 18:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31964/discussion-between-surase-prasad-and-em-sta) – Prasad Surase Jun 18 '13 at 18:52
16

Even the question is quite old, but you are missing the new that is crucial to this question. The methods destroy and create doesn't have anything with this issue. If you have a new method, which looks something like this:

class TreatmentsController < ApplicationController
  def new
    @patient = Patient.new
  end
end

Then the solution would be do modify the new method to "build" the paintings like this:

class TreatmentsController < ApplicationController
  def new
    @patient = Patient.new
    @patient.paintings.build
  end
end
Aleks
  • 4,866
  • 3
  • 38
  • 69
  • yes, but these demo codes has no `.build` action :-( https://github.com/nathanvda/cocoon_simple_form_demo/blob/master/app/controllers/projects_controller.rb#L26 – zx1986 Jul 14 '17 at 03:18
  • @zx1986 What do you mean by `they don't have build action`? I mean it shouldn't have the build action. The `build` is just a method to for the object. – Aleks Jul 14 '17 at 14:07
11

Try doing following in new action in controller

@patient.treatments.build

Check out build_association part http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to

You should also read about nested attributes. Use those for reference http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63
9

Dumb error, but I was using:

<% f.fields_for :partner do |fp| %>

instead of:

<%= f.fields_for :partner do |fp| %>
Alter Lagos
  • 12,090
  • 1
  • 70
  • 92
  • 2
    I have made the same mistake in `haml` file and wondering what the heck is wrong with my form for like 30 minutes... Oh my God... I use `- f.fields_for :partner do |fp|` instead of `= f.fields_for :partner do |fp|` – Fatima Sep 15 '21 at 08:25
0

is case you have an association where it is optional:

in the controller:

@associated_model = @model.associated_model || @model.build_associated_model

in the view:

<%= form.fields_for :associated_model do |am| %>