Rails 3.2 fields_for没有显示在has_one和accepts_nested_attributes_for上

时间:2016-07-13 20:57:45

标签: ruby-on-rails-3 fields-for nested-form-for

我在rails 3.2中使用gem'nested_form'

型号:

meeting_agenda.rb:

class MeetingAgenda < ActiveRecord::Base
  has_many :meeting_questions, :inverse_of => :meeting_agenda
  accepts_nested_attributes_for :meeting_questions, allow_destroy: true
  attr_accessible :meeting_questions_attributes
end

meeting_question.rb:

class MeetingQuestion < ActiveRecord::Base
  has_one :meeting_answer, :inverse_of => :meeting_question
  accepts_nested_attributes_for :meeting_answer
  attr_accessible :meeting_answer_attributes
end

meeting_answer.rb

class MeetingAnswer < ActiveRecord::Base
  belongs_to :meeting_question
end

控制器:

class MeetingProtocolsController < ApplicationController

  def new
    @agenda = MeetingAgenda.new
  rescue => error
    render_403
  end


  def create
    @agenda = MeetingAgenda.new(params[:meeting_agenda])

    if @agenda.save && protocol.save
      flash[:notice] = l(:notice_successful_create)
      redirect_to action: 'show', id: @agenda.id
    else
      render action: 'new'
    end
end

查看:

new.html.haml:

= nested_form_for @agenda do |f|
# ...

  = f.fields_for :meeting_questions do |question|
    # ...

    # Fields for answer is not showing!
    = question.fields_for :meeting_answer do |answer| # <-- blank

      %p # <-- blank
        = answer.label :reporter_id_is_contact, t(:label_meeting_question_user_is_contact) # <-- blank
        = answer.check_box :reporter_id_is_contact; # <-- blank

        = question.link_to_remove l(:button_delete) # <-- blank

  = f.link_to_add l(:button_add), :meeting_questions
  = submit_tag l(:button_create)

has_oneaccepts_nested_attributes_for的关系为空。

相同的形式在rails 5上运行良好。

0 个答案:

没有答案