mercredi 13 juillet 2016

Rails 3.2 fields_for not showing

I use gem 'nested_form' in rails 3.2

Models:

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

Controller:

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

View:

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)

I`m try to resolve this problem for 3 days =( has_one relation with accepts_nested_attributes_for is empty. Help me, please!

Aucun commentaire:

Enregistrer un commentaire