0

I'm currently trying to build a really simple nested form app in my quest to learn rails. In this app I have three models legal_form answer and question. I have my my answers.html.erb, set up as follows:

<%= form_for (@legal_form) do |f| %>
<h1>Title <%= @legal_form.title %></h1>
<p><%= @legal_form.description %></p>
<ul>
  <% @legal_form.questions.each do |question| %>
    <%= fields_for question.answers.build do |q| %>
      <li>
        <%= question.question_content %>
        <%= q.text_field :answer_content %>
      </li>
    <% end =%>
  <% end %>
</ul>

<p><%= f.submit "Submit" %></p>
<% end %>

Which currently grabs the three questions I have stored and renders text input boxes next to them; works without a problem. However, when I submit the values, I get the "param is missing or empty: legal_form".

I figure that this is most likely due to my strong params configuration in the legal_forms controller, see below.

class LegalFormsController < ApplicationController
  before_action :find_legal_form, only: [:show, :edit, :update, :destroy, :answers]

  def index
    @legal_form = LegalForm.all.order("created_at DESC")
  end

  def show
  end

  def new
    @legal_form=LegalForm.new
  end

  def create
    @legal_form = LegalForm.new(legal_form_params)

    if @legal_form.save
      redirect_to @legal_form, notice: "Successfully created new legal form."
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @legal_form.update(legal_form_params)
      redirect_to @legal_form
    else
      render 'edit'
    end
  end

  def destroy
    @legal_form.destroy
    redirect_to root_path, notice: "Successfully deleted form"
  end

  def answers
    @questions=@legal_form.questions
    @legal_form=LegalForm.find(params[:id])
  end
  private

  def legal_form_params
    params.reqire(:legal_form).permit(:title, :description, :questions_attribute => [:id, :question_number, :question_content, :_destroy, :answer_attributes => [:id, :answer_content, :question_id, :user_id]])
  end

  def find_legal_form
    @legal_form=LegalForm.find(params[:id])
  end
end

And, in case it's helpful, here are the models for each.

class Answer < ActiveRecord::Base
  belongs_to :question
end

class LegalForm < ActiveRecord::Base
  has_many :questions, :dependent => :destroy

  has_many :answers, through: :entity_roles

  accepts_nested_attributes_for :questions,
  reject_if: proc { |attributes| attributes['question_content'].blank? },
  allow_destroy: true

end

class Question < ActiveRecord::Base
  belongs_to :legal_form
  has_many :answers
  accepts_nested_attributes_for :answers,
  reject_if: proc { |attributes| attributes['question_content'].blank? },
  allow_destroy: true
end

Also, as requested here's my routes file:

Rails.application.routes.draw do
  resources :legal_forms do
    member do
      get 'answers'
    end
  end

  resources :answers

  root "legal_forms#index"
end

Any help to finally conquer nested forms would be greatly appreciated. I've been banging my head against it off and on for about a week now. Many thanks in advance.

5
  • please show your routes.rb, and full legal_forms controller. Commented Dec 5, 2014 at 8:54
  • @Зелёный Many thanks for the response! I have added both to the question above. Commented Dec 5, 2014 at 9:06
  • Please fix indentation. that help us read this code. Commented Dec 5, 2014 at 9:09
  • I don't know if this would help, but why are you using the full name @legal_form instead of f? <%= form_for (@legal_form) do |f| %> <h1>Title <%= f.title %></h1> <p><%= f.description %></p> Commented Dec 5, 2014 at 9:11
  • @Зелёный Done, sorry to make reading it harder than it needs to be; sloppy indentation is indeed a pain to deal with. Commented Dec 5, 2014 at 10:24

1 Answer 1

1

Try in the controller

def legal_form_params
  params.require(:legal_form).permit(...)
end

Also question.answers.build add it to the method of your controller and call the object that returns the responses to fields_for

UPDATE

To be sent through this form your results, should probably be like this

form

<%= f.fields_for :answers do |q| %>
      ...
<% end =%>

in the new method

 def new
    @legal_form=LegalForm.new
    @answers = @legal_form.question.answers.build
 end

def legal_form_params
   params.require(:legal_form).permit! #temporarily
end

not tried it, but imagine how it works something like this

Sign up to request clarification or add additional context in comments.

3 Comments

good point, did not notice at first. That should be right answer.
thanks for the answer Anthony. Unfortunately it was already set to require on my server; the permit(:legal_form) was an error on my part when I copied it over. I'm afraid that I don't quite understand the second half of your answer regarding the inclusion of "the object that returns the responses to fields_for" Many thanks for the help!
Try doing what I wrote, it can help you a bit Unfortunately I cannot try it now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.