嵌套表单fields_for text_area未显示

时间:2021-12-31 14:31:35

I have three-tier model:

我有三层模型:

User has_many Asks has_many Outcomes

用户has_many要求has_many结果

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.

在主页上,我希望用户能够在他们标记完成时向他们的Ask添加结果。我正在尝试使用嵌套表单在Ask表单中显示Outcome描述,该表单还会更新done标志和完成日期。

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?

像SO上的其他用户/问题一样,我无法在屏幕上显示嵌套表格。我已经按照其他问题的说明进行操作,但仍然没有显示嵌套字段。我想知道是否有人可以在下面的代码中发现问题?

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 %>

1 个解决方案

#1


3  

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):

当在form_for和fields_for中使用符号时,Rails尝试使用具有相同名称的实例变量,例如@outcomes for:results。所以尝试(对于现有结果):

<% @outcomes = a.outcomes %>

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

在f.fields_for行之前:结果....

And for new outcomes:

并为了新的结果:

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

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

(最后一个对问题所有者的贡献)

#1


3  

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):

当在form_for和fields_for中使用符号时,Rails尝试使用具有相同名称的实例变量,例如@outcomes for:results。所以尝试(对于现有结果):

<% @outcomes = a.outcomes %>

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

在f.fields_for行之前:结果....

And for new outcomes:

并为了新的结果:

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

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

(最后一个对问题所有者的贡献)