Search code examples
ruby-on-railsruby-on-rails-4nested-formsnested-form-for

Invalid association. Make sure that accepts_nested_attributes_for is used for :questions association


i am building nested form in rails 4.I keep getting this error

my _form.html.erb as

    <%= nested_form_for (@project) do |f| %>
    <% if @project.errors.any? %>
    <div id="error_explanation">
    <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

    <ul>
    <% @project.errors.full_messages.each do |message| %>
      <li><%= message %></li>
     <% end %>
     </ul>
     </div>
    <% end %>

    <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
    </div>

    <%= f.fields_for :questions do |builder| %>
    <%= render "question_fields", :f => builder %>
    <% end %>

    <div class="actions">
    <%= f.submit %>
    </div>
    <% end %>

_question.html.erb

      <p>
            <%= f.label :content, "Question" %><br />
            <%= f.text_area :content, :rows => 3 %><br />
            <%= f.label :subject, "Question" %><br />
            <%= f.text_field :subject %><br />
            <%= f.link_to_remove "Remove this task" %>
            <p><%= f.link_to_add "Add a questions",:questions %></p>
     </p>

project.rb

       has_many :questions, :dependent => :destroy

       accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

Edit

question.rb

      class Question < ActiveRecord::Base

       belongs_to :project

       end

projects controller

      def new
      @project = Project.new
       3.times do
       question = @project.questions.build
        end
      def project_params
      params.require(:project).permit(:name)
      end

        def create
        @project = Project.new(project_params)

        respond_to do |format|
        if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
         format.json { render :show, status: :created, location: @project }
        else
         format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
       end
      end

end

I used "nested_form" gem Gives Error Invalid association. Make sure that accepts_nested_attributes_for is used for :questions association.

pls help me to get rid of this error

questions controller def:-

      def question_params
      params.require(:question).permit(:project_id, :subject, :content)
      end

Solution

  • Might be your problem is with your strong parameters

    Try changing your project_params in your projects_controller as

    def project_params
      params.require(:project).permit(:name,questions_attributes: [:project_id,:subject,:content])
    end
    

    And also,your controller code should look like this

    def new
      @project = Project.new
      3.times do
        @question = @project.questions.build
      end
    end
    
    def create
      @project = Project.new(project_params)
    
      respond_to do |format|
        if @project.save
          format.html { redirect_to @project, notice: 'Project was successfully created.' }
          format.json { render :show, status: :created, location: @project }
        else
          format.html { render :new }
          format.json { render json: @project.errors, status: :unprocessable_entity }
        end
      end
    end
    
    private
    
    def project_params
      params.require(:project).permit(:name,questions_attributes: [:project_id,:subject,:content])       
    end
    

    Also,you have to look for Strong Parameters with accepts_nested_attributes_for.

    Update

    Try changing your _form.html.erb as

    <%= nested_form_for(@project) do |f| %>
      <% if @project.errors.any? %>
        <div id="error_explanation">
        <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
    
        <ul>
        <% @project.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
        </ul>
        </div>
      <% end %>
    
      <div class="field">
        <%= f.label :name %><br>
        <%= f.text_field :name %>
      </div>
    
      <%= f.fields_for :questions do |builder| %>
        <%= render "question_fields", :ff => builder %> #changed to ff to avoid confusion
        <% end %>
        <p><%= f.link_to_add "Add a questions",:questions %></p> #this line it should be here.
        <div class="actions">
        <%= f.submit %>
        </div>
      <% end %>
    

    And your _question_fields.html.erb as

    <p>
      <%= ff.label :content, "Question" %><br />
      <%= ff.text_area :content, :rows => 3 %><br />
      <%= ff.label :subject, "Question" %><br />
      <%= ff.text_field :subject %><br />
      <%= ff.link_to_remove "Remove this task" %>       
    </p>