Unexpected Rollback upon saving form

402 views Asked by At

I am having a bunch of trouble saving a form with nested attributes. I'm working with two models Submission and Tag. Each Submission has_one :tag.

The issue that I'm having is that when I submit the form, I'm getting a "rollback" and the data is not saving:

Processing by SubmissionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"XSGZaJ1nR2MbkJJZS/Ftkb9jefb9FLOAJPA1nYbG/AIrVJYb93DtPxc7fzhUFt9uSnzzcNGqkMTSz/jFsFejHA==", "submission"=>{"domain"=>"test", "tag_attributes"=>{"tag_text"=>"Junk Science", "notes"=>"test"}}, "commit"=>"Submit"}
   (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
  Rendering submissions/new.html.erb within layouts/application
  Rendered submissions/_form.html.erb (10.6ms)
  Rendered submissions/new.html.erb within layouts/application (11.8ms)
  Rendered shared/_navbar.html.erb (2.7ms)
Completed 200 OK in 2276ms (Views: 2269.6ms | ActiveRecord: 0.2ms)

My controller is as follows:

  def new
    @submission = Submission.new
    @tag = @submission.build_tag
  end

  def create
    @submission = Submission.new(submission_params)

    if @submission.save
      redirect_to root_path, notice: "Thank you for your submission."
    else
      render 'new'
    end
  end

  private

  def submission_params
    params.require(:submission).permit(:subtitle, :domain, :type1, :type2, :type3, :website_type, :user_id, :notes, tag_attributes:[:tag_text, :notes])
  end

and my form is as follows:

<%= simple_form_for @submission, defaults: { wrapper_html: {class: 'form-group'}, input_html: { class: "form-control" }} do |f| %>

    <%= f.input :domain, required: true, input_html: {class: "form-control"}%>
    <%= f.simple_fields_for :tag do |tag| %>
      <%= tag.input :tag_text, collection: source_type %>
      <%= tag.input :notes %>
    <% end %>

    <%= f.submit "Submit", class: 'btn btn-primary'%>
<% end %>

Thanks for any suggestions.

Update 1: The output of logger.debug @submission.errors.inspect is as follows:

 #<ActiveModel::Errors:0x007ff49e5f8e90 @base=#<Submission id: nil, domain: "test", type1: nil, type2: nil, type3: nil, website_type: nil, user_id: nil, notes: nil, created_at:     nil, updated_at: nil>, @messages={:"tag.submission"=>["must exist"]}, @details={"tag.submission"=>[{:error=>:blank}]}>

There are a good deal of blank fields under submission but it doesn't start throwing a fit until I try adding the nested attributes.

Update2: My Tag model is as follows:

class Tag < ApplicationRecord
  belongs_to :submission
end

My Submission model is as follows:

class Submission < ApplicationRecord
    has_one :tag
    accepts_nested_attributes_for :tag
end
1

There are 1 answers

0
neanderslob On BEST ANSWER

Ultimately, I just reverted the version of rails to Rails4 and everything. worked.