user3787971
user3787971

Reputation: 457

How to get a nested text_area to render properly

I have a scaffold called submits that has a form for creating new submissions. I also created a model called question. I've used this model to create different questions in the submission form.I've utilized a join form and use active admin to add/edit questions from the backend. I'm getting this error.

undefined method `submit[question_ids][]' for #<Submit id: nil, name: nil, created_at: nil, updated_at: nil>

submits.rb

class Submit < ActiveRecord::Base
 has_and_belongs_to_many :questions
end

question.rb

class Question < ActiveRecord::Base
 has_and_belongs_to_many :submits
end

subits/_form.html.erb

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

  <ul>
  <% @submit.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
</div>
<div class="field">
  <%= f.label :name,"Team Name" %><br>
  <%= f.text_field :name, class: "form-control"  %>
</div>

<% @questions.each do |question| %>
  <div class="field">
    <%= f.label(question.question)%>
    <%= f.text_area "submit[question_ids][]" %>
  </div>
<% end %> 

  <div class="actions">
   <%= f.submit "Apply", class: "btn btn-primary btn-lg" %>
  </div>
<% end %>

I'm guessing my error is here:

 <%= f.text_area "submit[question_ids][]" %> 

I'm just not sure what the correct syntax is. Any suggestions?

Upvotes: 0

Views: 58

Answers (1)

angelica
angelica

Reputation: 36

For Rails form_for input forms, you need :(whatever attribute) which should be defined in your migration file.

ex.) if you have 'text' attribute in your Submit model you can have your input form for the text attribute just like this. <%= f.text_area :text %>

But in this case it seems like you have a join table for your models, so I think you should fields_for for your join table.

cf.) How do i include Rails join table field in the form?

Upvotes: 1

Related Questions