Hamidreza-Safari
Hamidreza-Safari

Reputation: 83

Could not save nested form in rails 5.1

I used nested_form gem and I am trying to build a form which has fields from two tables(Project, Question).

My model:

class Project < ApplicationRecord
    has_many :questions
    accepts_nested_attributes_for :questions
end

class Question < ApplicationRecord
    belongs_to :project
end

My controller:

class ProjectsController < ApplicationController
layout 'application'

def index
    @projects = Project.all
end

def show
    @project = Project.find(params[:id])
end

def new
    @project = Project.new
    @questions = @project.questions.build
end

def create
@project = Project.new(project_params)

@project.save
 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, question_attributes: [:id,   :content, :_delete])
end
end

My view:

<%= 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 %> 
   <% end %>
   <p>
    <%= f.link_to_add "Add a questions",:questions %>
   </p> 
   <div class="actions">
    <%= f.submit %>
   </div>
<% end %>

And my schema file :

create_table "projects", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "questions", force: :cascade do |t|
t.integer "project_id"
t.string "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["project_id"], name: "index_questions_on_project_id", using: :btree
end

And _question_fields file is :

<p>
 <%= ff.label :content, "Question" %>
 <%= ff.text_area :content%>
 <%= ff.link_to_remove "Remove this task"%>
</p>

Table Project will be saved but the table Question could not be saved.Why?

After change this line of code

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

I get the following error:

1 error prohibited this project from being saved:

Questions project must exist

I applied the change too ,but this time nothing could not be saved.

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

Upvotes: 2

Views: 97

Answers (1)

Pavan
Pavan

Reputation: 33542

The issue is with the project_params. As you have has_many :questions, the question_attributes should be changed to questions_attributes

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

Update:

Questions project must exist

Either permit project_id in questions_attributes

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

or set optional: true on the association

class Question < ApplicationRecord
  belongs_to :project, optional: true
end

Upvotes: 2

Related Questions