kwh941
kwh941

Reputation: 235

Nested form fields_for text_area is not displaying

I have three-tier model:

User has_many Asks has_many Outcomes

On the home page, I would like the user to be able to add an Outcome to their Ask when they mark it complete. I'm trying to use a nested form to display the Outcome description in the Ask form which also updates the done flag and done date.

Like other users/questions here on SO, I cannot get a nested form to display on the screen. I've followed instructions from the other questions, but still the nested field is not displaying. Am wondering if someone can spot the issue in the code below?

Ask Model

class Ask < ActiveRecord::Base

  attr_accessible   :category, :description, :done, :followed_up, 
                    :helper, :public, :date_done, :date_followed_up, :user_id, :outcomes_attributes
  belongs_to :user, counter_cache: true
  has_many :outcomes
  accepts_nested_attributes_for :outcomes

end

Ask Controller

class AsksController < ApplicationController

  def new
    @ask = current_user.asks.build(params[:ask])
    @ask.outcomes.build
  end

  def create
    @ask = current_user.asks.build(params[:ask])
    if @ask.save!
      respond_to do |format|
        format.html { redirect_to edit_ask_path(@ask) }
        format.js
      end
    else
      flash[:error] = "Something is wrong. The Ask was not saved..."
    end
  end

  def edit
    @ask = current_user.asks.find(params[:id])
  end

  def update
    @ask = current_user.asks.find(params[:id])
    @ask.outcomes.build
    @ask.update_attributes(params[:ask])
    respond_to do |format|
      format.html { redirect_to edit_ask_path(@ask) }
      format.js
    end
  end
end

Home Page Controller (this form is on the home page)

class StaticPagesController < ApplicationController

  def home
    if signed_in?
      @ask = current_user.asks.build(params[:ask])
      @ask.outcomes.build
    end
  end

Form Partial rendered on the home page

<% if current_user.asks.any? %>
  <ul id="ask-list-items">
    <% current_user.asks.where(done: false).each do |a| %> 
          <%= form_for(a) do |f| %>
            <li><%= a.description %></li>
            <%= f.hidden_field :date_done, value: Date.today %>
            <%= f.hidden_field :done, :value=>true %>
            <%= f.submit "Mark as done", class: "btn btn-small hidden done_btn", id: "a-#{a.id}-done" %>

            <%= f.fields_for :outcomes do |builder| %> # << These fields are not showing up
              <%= builder.text_area :description, placeholder: "Describe the outcome...", id: "ask-message" %>
            <% end %>
            <%= f.submit "Save outcome", class: "btn btn-primary" %>
          <% end %>
    <% end %>
  </ul>
<% end %>

Upvotes: 0

Views: 885

Answers (1)

244an
244an

Reputation: 1589

When using symbol in form_for and fields_for Rails tries to use an instance variable with he same name, e.g. @outcomes for :outcomes. So try (for existing outcomes):

<% @outcomes = a.outcomes %>

before the line with f.fields_for :outcomes....

And for new outcomes:

<% @outcomes = a.outcomes.build %>

(the last with contribution to the owner of the question)

Upvotes: 3

Related Questions