Reputation: 69
Sorry for the Noob question. I have a form that allows a user to create a resume. I created a Resume scaffold with inputs for title and career objective. I made separate database tables for the other sections (experience, education, skills). I want to have all of this information submitted using one single form being the resumes/views/_form.html
with the resume controller. So in the Resume Model I put ( has_many :experiences
) and in the Experience Model I put ( belongs_to :resume
). My Experience table has two fields (:job_title
, :job_description
), so in the views/resumes/_form.html
I added two additional fields for :job_title
and :job_description
. I'm kind of lost at this point. How do I save those input values to the appropriate database tables using the Resume controllers 'new' and 'create' methods? I know I'm missing code right now, but I'm getting this error if it's any help:
undefined method `job_title' for #<Resume:0x007fe5b2304748>
Trace of template inclusion: app/views/resumes/new.html.erb
app/views/resumes/_form.html.erb:24:in `block in _app_views_resumes__form_html_erb___303730996527532694_70312260165660'
app/views/resumes/_form.html.erb:1:in `_app_views_resumes__form_html_erb___303730996527532694_70312260165660'
app/views/resumes/new.html.erb:3:in `_app_views_resumes_new_html_erb___2010250825676564736_70312281682320'
Here is the /resumes/_form.html.erb view
<%= form_for(@resume) do |f| %>
<% if @resume.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@resume.errors.count, "error") %> prohibited this resume from being saved:</h2>
<ul>
<% @resume.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :objective %><br>
<%= f.text_area :objective %>
</div>
<%= fields_for @experience do |fa| %>
<div class="field">
<%= fa.label :job_title %><br>
<%= fa.text_area :job_title %>
</div>
<div class="field">
<%= fa.label :company_name %><br>
<%= fa.text_area :company_name %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The resumes_controller
class ResumesController < ApplicationController
before_action :set_resume, only: [:show, :edit, :update, :destroy]
# GET /resumes
# GET /resumes.json
def index
@resumes = Resume.all
end
# GET /resumes/1
# GET /resumes/1.json
def show
end
# GET /resumes/new
def new
@resume = Resume.new
end
# GET /resumes/1/edit
def edit
end
# POST /resumes
# POST /resumes.json
def create
@resume = Resume.new(resume_params)
respond_to do |format|
if @resume.save
format.html { redirect_to @resume, notice: 'Resume was successfully created.' }
format.json { render action: 'show', status: :created, location: @resume }
else
format.html { render action: 'new' }
format.json { render json: @resume.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /resumes/1
# PATCH/PUT /resumes/1.json
def update
respond_to do |format|
if @resume.update(resume_params)
format.html { redirect_to @resume, notice: 'Resume was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @resume.errors, status: :unprocessable_entity }
end
end
end
# DELETE /resumes/1
# DELETE /resumes/1.json
def destroy
@resume.destroy
respond_to do |format|
format.html { redirect_to resumes_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_resume
@resume = Resume.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def resume_params
params.require(:resume).permit(:title, :objective)
end
end
Upvotes: 0
Views: 641
Reputation: 1227
I think you need to change
<%= fields_for @experience do |fa| %>
to
<%= f.fields_for @experience do |fa| %>
And make sure you have accepts_nested_attributes_for
set up correctly in the models.
Upvotes: 1