I'm not getting the expected results, the Skills are not getting saved. I want to allow each person to have say a primary skill ONLY 1.
DB
# Table name: people
#
# id :integer not null, primary key
# first_name :string(255)
# last_name :string(255)
# headline :string(255)
# description :string(255)
# user_id :integer
# created_at :datetime
# updated_at :datetime
#
# == Schema Information
#
# Table name: skills
#
# id :integer not null, primary key
# title :string(255)
# description :text
# created_at :datetime
# updated_at :datetime
#
# == Schema Information
#
# Table name: entity_skills
#
# id :integer not null, primary key
# skill_id :integer
# person_id :integer
# created_at :datetime
# updated_at :datetime
#
Models
class Person < ActiveRecord::Base
has_many :entity_skills
has_many :skills, through: :entity_skills, foreign_key: "person_id"
accepts_nested_attributes_for :skills
end
class Skill < ActiveRecord::Base
has_many :entity_skills
has_many :people, through: :entity_skills, foreign_key: "person_id"
end
class EntitySkill < ActiveRecord::Base
belongs_to :person
belongs_to :skill
end
Controller
def new
#person = Person.new
#all_skills = Skill.all
#entity_skills = #person.skills.build
end
def edit
#all_skills = Skill.all
#entity_skills = #person.entity_skills.build
end
def create
#person = Person.new(person_params)
respond_to do |format|
if #person.save
format.html { redirect_to #person, notice: 'Person was successfully created.' }
format.json { render action: 'show', status: :created, location: #person }
else
format.html { render action: 'new' }
format.json { render json: #person.errors, status: :unprocessable_entity }
end
end
end
def person_params
params.require(:person).permit(:first_name, :last_name, :headline, :description, :user_id, :employer_id, skills_attributes: [:id])
end
Form
<%= form_for(#person) do |f| %>
....
....
<h2>Skills</h2>
<%= f.fields_for #entity_skills do |es| %>
<%= es.label "All Skills" %>
<%= collection_select(:skills, :id, #all_skills, :id, :title) %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Nothing is being saved for the skills, Can someone explain what is going on?
DB
# == Schema Information
#
# Table name: entity_skills
#
# id :integer not null, primary key
# skill_id :integer
# person_id :integer
# created_at :datetime
# updated_at :datetime
# position :integer
#
Controller
def new
#person = Person.new
#all_skills = Skill.all
5.times {#person.entity_skills.build}
end
def create
#person = Person.new(person_params)
respond_to do |format|
if #person.save
format.html { redirect_to #person, notice: 'Person was successfully created.' }
format.json { render action: 'show', status: :created, location: #person }
else
format.html { render action: 'new' }
format.json { render json: #person.errors, status: :unprocessable_entity }
end
end
end
def person_params
params.require(:person).permit(:first_name, :last_name, :headline, :description, :user_id, :employer_id, :entity_skills_attributes => [:skill_id, :position, :person_id, :id])
end
Form
<h2>Skills</h2>
<% i=0 %>
<%= f.fields_for :entity_skills do |builder| %>
<% i+=1 %>
<%= render 'shared/skills_fields', :f => builder, :i=> i %>
<% end %>
shared/skills_fields
<div class="field">
<%= f.label "All Skills" %>
<%= f.collection_select(:skill_id, Skill.all, :id, :title) %>
<%= f.hidden_field :position, :value=>i %>
<%= f.hidden_field :person_id, :value=>#person.id %>
</div>
I had a few things funky, params were accepted but not assigned correctly to the form, also made mistake of using :skills in the permitted params instead of :entity_skills_attributes => [:skill_id, :position]
To work with updates all I had to do was
def edit
#entity_skills= #person.entity_skills
end
I will use the position to reorder skills in case anyone wonders ;-)
Finally solved
You cant save a has_many through without constructing the join records.
Perhaps checkout this tutorial by Dave Shepard which runs through the things you need to do with a full example.
Related
For my project I am trying to build a dashboard whereby an Agent can view submissions posted by a user and add a Status & Notes to each submission in order to log their own personal activity i.e they would not be changing the actual record, just leaving private notes against it. In order to do this I have created a join table with both Agent id and Submission id as well as Status and Notes columns.
I have managed to create an index view that shows submissions data with 2 form fields at the end of each line from my join table which are called Status and Notes... the problem is when I update these fields they do not get saved to my jointable.
Form on index view
<%= form_with(model: submission, local: true) do |form| %>
<% form.fields_for :agent_activities do |act| %>
<td> <div class="field">
<%= act.text_field :Status %>
</div>
</td>
<td> <div class="field">
<%= act.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
<% end %>
<% end %>
Model associations in rb files
class Submission < ApplicationRecord
belongs_to :user, :optional => true
belongs_to :location, :optional => true
has_many :agent_activities
end
class AgentActivity < ApplicationRecord
belongs_to :submission, :optional => true #has submission_id
foreign key in table
belongs_to :agent, :optional => true #has agent_id foreign key in
table
end
Controller:
class SubmissionsController < ApplicationController
before_action :set_submission, only: [:show, :edit, :update, :destroy]
def index
#submissions = Submission.where(:user_id => current_user.id)
end
def show
end
def new
#submission = Submission.new
end
def edit
end
# POST /submissions
# POST /submissions.json
def create
#submission = Submission.new(submission_params.merge(user_id: current_user.id))
respond_to do |format|
if #submission.save
# Tell the UserMailer to send a welcome email after save
NewSubmissionMailer.submission_email(#submission).deliver_now
NewSubmissionMailer.matching_agents_email(#submission).deliver_now
format.html { redirect_to #submission, notice: 'Submission was successfully created.' }
format.json { render :show, status: :created, location: #submission }
else
format.html { render :new }
format.json { render json: #submission.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /submissions/1
# PATCH/PUT /submissions/1.json
def update
respond_to do |format|
if #submission.update(submission_params)
format.html { redirect_to #submission, notice: 'Submission was successfully updated.' }
format.json { render :show, status: :ok, location: #submission }
else
format.html { render :edit }
format.json { render json: #submission.errors, status: :unprocessable_entity }
end
end
end
# DELETE /submissions/1
# DELETE /submissions/1.json
def destroy
#submission.destroy
respond_to do |format|
format.html { redirect_to submissions_url, notice: 'Submission was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_submission
#submission = Submission.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def submission_params
params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements)
end
end
Not sure what im missing here :/
UPDATE BASED OFF #TOM ANSWER
New controller params:
def submission_params
params.require(:submission).permit(:First_Name, :Last_Name, :Phone, :Email, :Desired_Location, :number_of_beds, :number_of_occupants, :Rent_price_per_month_gbp, :Max_move_in_date, :Tenant_Occupation, :Contact_me_on, :Furnished, :Current_Address, :Property_Requirements, agent_activities_attributes: [:id, :Status, :Notes, :_destroy])
end
end
New Submission Model rb:
class Submission < ApplicationRecord
belongs_to :user, :optional => true
belongs_to :location, :optional => true
has_many :agent_activities
accepts_nested_attributes_for :agent_activities
end
Index.html.erb
<%= form_with(model: submission, local: true) do |form| %>
<% form.fields_for :agent_activities, #submission.agent_activities.build do |act| %>
<td> <div class="field">
<%= act.text_field :Status %>
</div>
</td>
<td> <div class="field">
<%= act.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= form.submit %>
</div>
</td>
<% end %>
On your Submission model add: accepts_nested_attributes_for :agent_activities (accepts_nested_attributes_for documentation) This will let Rails know that your form is going to be supplying fields for an associated model.
Once that is added Rails will be supplying a key in params agent_activities_attributes in your strong params we can add: .permit(..., agent_activities_attributes: [:id, :Status, :Notes, :_destroy]. The :_destroy key is only needed if you plan on having allow_destroy: true on the nested attribute call.
One side note: Capitalized names (Status, Notes, etc) are normally reserved for constants in Ruby. You may want to look into changing your attribute column names to lowercase.
During the update action of a nested form, instead of updating the current nested records, it seems to create new nested records.
This is what my controller looks like :
class ApplicationsController < ApplicationController
before_filter :set_user_and_job
def new
job = params[:job_id]
#application = Application.build(job)
end
def create
#application = Application.new(application_params)
#application.save
redirect_to root_url, :notice => "You have now applied!"
end
def edit
#application = Application.find(params[:id])
#answers = []
#job.questions.each do |question|
#application.answers.each do |answer|
#answers << answer if answer.question_id == question.id
end
end
end
def update
#application = Application.find(params[:id])
#application.update_attributes(application_params)
redirect_to root_url, :notice => "You have updated your application!"
end
def destroy
Application.find(params[:id]).destroy
flash[:success] = "Application Deleted."
redirect_to root_url
end
def show
#application = Application.find(params[:id])
#answers = []
#job.questions.each do |question|
#application.answers.each do |answer|
#answers << answer if answer.question_id == question.id
end
end
end
private
def set_user_and_job
#user = current_user
#job = Job.find(params[:job_id])
end
def application_params
params.require(:application).permit(:job_id, :user_id, answers_attributes:[:question_id, :content]).merge(user_id: current_user.id, job_id: params[:job_id])
end
end
This is what my edit view looks like :
<% provide(:title, " Edit this application") %>
<div class="row">
<div class="span6">
<h2> Job: <%= #job.job_title %></h2>
<p> <%= #job.job_summary %> </p>
</div>
<div class="span6">
<h2> Applicant: <%= #user.name %></h2>
</div>
<div class="span12">
<h3>Edit your job application below.</h3>
</div>
</div>
<%= form_for [#job, #application] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<% #job.questions.each_with_index do |question| %>
<%= f.fields_for :answers, question do |question_field| %>
<%= question_field.label :content, question.content %>
<%= question_field.text_area :content, :value => "" %>
<%= question_field.hidden_field :question_id, :value => question.id %>
<% end %>
<% end %>
<%= f.submit "Submit the application", class: "button" %>
<% end %>
The Application Model itself:
# == Schema Information
#
# Table name: applications
#
# id :integer not null, primary key
# user_id :integer
# job_id :integer
# created_at :datetime
# updated_at :datetime
#
class Application < ActiveRecord::Base
belongs_to :job
belongs_to :user
validates :job_id, presence: true
validates :user_id, presence: true
has_many :answers
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
def self.build(job_id)
application = self.new
job = Job.find(job_id)
job.questions.count.times do
application.answers.build
end
application
end
end
And the Answer Model :
# == Schema Information
#
# Table name: answers
#
# id :integer not null, primary key
# application_id :integer
# question_id :integer
# created_at :datetime
# updated_at :datetime
# content :string(255)
#
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :application
validates :content, presence: true
end
From searching, I found this link, RoR nested attributes produces duplicates when edit , which suggests that I add the :id to application_params, however when I do that, I get the error
ActiveRecord::RecordNotFound in ApplicationsController#update
Couldn't find Answer with ID=5 for Application with ID=17
(That's also a bit weird, because the actual id of the answer is 39. 5 is actually the ID of the question :S )
What are your thoughts on this? Mentors of SO, help much appreciated :)
update_only does not work for has_many relationships. You need to add the nested attribute :id field to your strong parameters:
def application_params
params.require(:application).permit(:job_id, :user_id,
answers_attributes:[:id, :question_id, :content]).merge(user_id: current_user.id,
job_id: params[:job_id])
end
Try adding update_only to your call to accepts_nested_attributes_for.
accepts_nested_attributes_for :nested_attribute, update_only: true
I have a nested form, and am having trouble getting the create and update function to work.
I think what's happening is that the nested object is being created first, and is causing an error.
I get this error when trying to run the create action:
ActiveRecord::RecordNotFound in ApplicationsController#create
Couldn't find Answer with ID=5 for Application with ID=
Rails.root: /Users/stepan/Desktop/Sites/atlas
Application Trace | Framework Trace | Full Trace
app/controllers/applications_controller.rb:22:in `create'
This only happened after I allowed the :id parameter for the answers model. The reason I did that was to make it possible for the update action to work.
This is what my Controller Looks like ->
class ApplicationsController < ApplicationController
def new
#job = Job.find(params[:job_id])
#user = current_user
#application = Application.new()
#job.questions.count.times do
#application.answers.build
end
end
def create
#user = current_user
#job = Job.find(params[:job_id])
#application = Application.new(application_params)
#application.job_id = #job.id
if #application.save
redirect_to root_url, :notice => "You have now applied!"
else
render :action => 'new'
end
end
def edit
#job = Job.find(params[:job_id])
#user = current_user
#application = Application.find(params[:id])
#answers = []
#job.questions.each do |question|
#application.answers.each do |answer|
#answers << answer if answer.question_id == question.id
end
end
end
def update
#job = Job.find(params[:job_id])
#user = current_user
#application = Application.find(params[:id])
if #application.update_attributes(application_params)
redirect_to root_url, :notice => "You have updated your application!"
else
render :action => 'new'
end
end
def destroy
Application.find(params[:id]).destroy
flash[:success] = "Application Deleted."
redirect_to root_url
end
def show
#job = Job.find(params[:job_id])
#user = current_user
#application = Application.find(params[:id])
#answers = []
#job.questions.each do |question|
#application.answers.each do |answer|
#answers << answer if answer.question_id == question.id
end
end
end
private
def application_params
params.require(:application).permit(:id, :job_id, :user_id, answers_attributes:[:content, :question_id, :id]).merge(user_id: current_user.id)
end
end
This is the form:
<% provide(:title, " Apply to this job") %>
<div class="row">
<div class="span6">
<h2> Job: <%= #job.job_title %></h2>
<p> <%= #job.job_summary %> </p>
</div>
<div class="span6">
<h2> Applicant: <%= #user.name %></h2>
</div>
<div class="span12">
<h3>You're almost done! Answer the questions below and you'll be applied to the job.</h3>
</div>
</div>
<%= form_for [#job, #application] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<% #job.questions.each_with_index do |question| %>
<%= f.fields_for :answers, question do |question_field| %>
<%= question_field.label :content, question.content %>
<%= question_field.text_area :content, :value => "" %>
<%= question_field.hidden_field :question_id, :value => question.id %>
<% end %>
<% end %>
<%= f.submit "Submit the application", class: "button" %>
<% end %>
This is my Application Model:
# == Schema Information
#
# Table name: applications
#
# id :integer not null, primary key
# user_id :integer
# job_id :integer
# created_at :datetime
# updated_at :datetime
#
class Application < ActiveRecord::Base
belongs_to :job
belongs_to :user
validates :job_id, presence: true
validates :user_id, presence: true
has_many :answers
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
This is my answer model:
# == Schema Information
#
# Table name: answers
#
# id :integer not null, primary key
# application_id :integer
# question_id :integer
# created_at :datetime
# updated_at :datetime
# content :string(255)
#
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :application
validates :content, presence: true
end
There was a similar problem here --> Rails 4 Nested Attributes Unpermitted Parameters, But I don't quite understand what the answer is doing.
Help much appreciated!
Some issues you may benefit from looking into:
Application Create
Bluntly, I think you're trying to do too much with the application create function
There's a principle in Rails (and all MVC) called fat model skinny controller, meaning you should put as many actions into the model as possible. The controller really just needs to guide logic (call respective methods depending on model responses). Here's what I'd do:
#app/controllers/applications_controller.rb
before_filter :set_user
def new
job_id = params[:job_id]
#application = Application.build(job_id)
end
def create
#application = Application.new(application_params)
#application.save
redirect_to root_url, :notice => "You have now applied!"
end
private
def set_user
#user = current_user
end
def application_params
params.require(:application).permit(:job_id, :user_id, answers_attributes:[:question_id, :content]).merge(user_id: current_user.id, job_id: params[:job_id])
end
#app/models/application.rb
Class Application < ActiveRecord::Base
#Associations here
accepts_nested_attributes_for :answers
validates :title, :other_application_vars,
presence: true #-> shows errors if validation fails (no need for "if #application.save" logic.... needs testing)
def self.build(job_id) #-> class method -- not sure about passing arguments
application = self.new
job = Job.find(job.id)
job.questions.count.times do
application.answers.build
end
application
end
end
#app/models/answer.rb
Class Answer < ActiveRecord::Base
validates :question_id, :content,
presence: true
end
Strong Params
You mentioned it will only allow you to do this if you send the id param through. I don't have a super amount of experience here, but I've found you don't need to send the :id param through strong params. This is probably what's causing the issue - you're not setting the id param, and consequently it's getting confused
If you adopt a conventional way of doing this, you shouldn't have this error
new rails user here. I'm trying to have my schedule form store an array of "days" but after several attempts I just can't make it work.
Here are my codes currently
*schedules/_form.html.erb:*
<%= simple_form_for #schedule do |f| %>
<% if #schedule.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#schedule.errors.count, "error") %> prohibited this schedule from being saved:</h2>
<ul>
<% #schedule.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :section_id do %>
<%= f.select :section_id, Section.all.map{|s| [s.seccon, s.id]}, :include_blank => true %>
<% end %>
<%= f.association :subject %>
<%= f.collection_select :day_ids, #days, :id, :name, {}, {:multiple => true, :size => 1} %>
<div class="field">
<%= f.label :start_time %>
<%= f.time_select :start_time %>
</div>
<%= f.input :professor do %>
<%= f.select :professor_id, Professor.all.map{|j| [j.procon, j.id]}, :include_blank => true %>
<% end %>
<%= f.association :room %>
<%= f.button :submit %>
<% end %>
*schedules_controller.rb:*
class SchedulesController < ApplicationController
before_action :set_schedule, only: [:show, :edit, :update, :destroy]
# GET /schedules
# GET /schedules.json
def index
#schedules = Schedule.all
#days = Day.all
end
# GET /schedules/1
# GET /schedules/1.json
def show
end
# GET /schedules/new
def new
#schedule = Schedule.new
#days = Day.all
end
# GET /schedules/1/edit
def edit
end
# POST /schedules
# POST /schedules.json
def create
#schedule = Schedule.new(schedule_params)
#days = Day.all
respond_to do |format|
if #schedule.save
format.html { redirect_to #schedule, notice: 'Schedule was successfully created.' }
format.json { render action: 'show', status: :created, location: #schedule }
else
format.html { render action: 'new' }
format.json { render json: #schedule.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schedules/1
# PATCH/PUT /schedules/1.json
def update
respond_to do |format|
if #schedule.update(schedule_params)
format.html { redirect_to #schedule, notice: 'Schedule was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #schedule.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schedules/1
# DELETE /schedules/1.json
def destroy
#schedule.destroy
respond_to do |format|
format.html { redirect_to schedules_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_schedule
#schedule = Schedule.find(params[:id])
#days = Day.all
end
# Never trust parameters from the scary internet, only allow the white list through.
def schedule_params
params.require(:schedule).permit(:section_id, :subject_id, :start_time, :finish_time_id, :professor_id, :room_id, :day_ids)
end
end
schedule.rb:
class Schedule < ActiveRecord::Base
belongs_to :section
belongs_to :subject
belongs_to :finish_time
has_and_belongs_to_many :days
accepts_nested_attributes_for :days, :allow_destroy => true
validates :section_id, :subject_id, :start_time, :professor_id, :room_id, :presence => true
belongs_to :professor
belongs_to :room
end
day.rb:
class Day < ActiveRecord::Base
has_and_belongs_to_many :schedules
default_scope { order(:id)}
has_paper_trail
validates :name, :desc, :presence => true
end
As was said here, the best thing to do is to create a has_many model relationship between Schedule and Day. You'll need a separate join table to make the relationship work. It will have: schedule_id and day_id as the two columns. You'd do this because you have a many > many relationship. There can be many schedules that belong to a day and many days that belong to a schedule.
I used this scenario in my app:
Recipe.rb
class Recipe < ActiveRecord::Base
has_and_belongs_to_many :wines
default_scope { order(:name) }
end
Wine.rb
class Wine < ActiveRecord::Base
has_and_belongs_to_many :recipes
accepts_nested_attributes_for :recipes, :allow_destroy => true
end
Migration
class AddRecipesWinesJoinTable < ActiveRecord::Migration
def self.up
create_table :recipes_wines, :id => false do |t|
t.column :recipe_id, :integer, :null => false
t.column :wine_id, :integer, :null => false
end
add_index :recipes_wines, [:wine_id]
end
def self.down
remove_index :recipes_wines, [:wine_id]
drop_table :recipes_wines
end
end
_wine_form.html.erb
# #recipes is Recipe.all generated by the controller
<%= w.collection_select :recipe_ids, #recipes, :id, :name, {}, {:multiple => true, :size => 6, :style => 'width:100%'} %>
Hope this helps.
I'm having a time trying to get this nested model working. I've tried all manner of pluralization/singular, removing the attr_accessible altogether, and who knows what else.
restaurant.rb:
# == RESTAURANT MODEL
#
# Table name: restaurants
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class Restaurant < ActiveRecord::Base
attr_accessible :name, :job_attributes
has_many :jobs
has_many :users, :through => :jobs
has_many :positions
accepts_nested_attributes_for :jobs, :allow_destroy => true
validates :name, presence: true
end
job.rb:
# == JOB MODEL
#
# Table name: jobs
#
# id :integer not null, primary key
# restaurant_id :integer
# shortname :string(255)
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Job < ActiveRecord::Base
attr_accessible :restaurant_id, :shortname, :user_id
belongs_to :user
belongs_to :restaurant
has_many :shifts
validates :name, presence: false
end
restaurants_controller.rb:
class RestaurantsController < ApplicationController
before_filter :logged_in, only: [:new_restaurant]
def new
#restaurant = Restaurant.new
#user = current_user
end
def create
#restaurant = Restaurant.new(params[:restaurant])
if #restaurant.save
flash[:success] = "Restaurant created."
redirect_to welcome_path
end
end
end
new.html.erb:
<% provide(:title, 'Restaurant') %>
<%= form_for #restaurant do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label "Restaurant Name" %>
<%= f.text_field :name %>
<%= f.fields_for :job do |child_f| %>
<%= child_f.label "Nickname" %>
<%= child_f.text_field :shortname %>
<% end %>
<%= f.submit "Done", class: "btn btn-large btn-primary" %>
<% end %>
Output Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"DjYvwkJeUhO06ds7bqshHsctS1M/Dth08rLlP2yQ7O0=",
"restaurant"=>{"name"=>"The Pink Door",
"job"=>{"shortname"=>"PD"}},
"commit"=>"Done"}
The error i'm receiving is:
ActiveModel::MassAssignmentSecurity::Error in RestaurantsController#create
Cant mass-assign protected attributes: job
Rails.root: /home/johnnyfive/Dropbox/Projects/sa
Application Trace | Framework Trace | Full Trace
app/controllers/restaurants_controller.rb:11:in `new'
app/controllers/restaurants_controller.rb:11:in `create'
Anyone have ANY clue how to get this to work? Thanks!
in restaurant.rb:
it should be
attr_accessible :name, :jobs_attributes
instead of
attr_accessible :name, :job_attributes
regarding your last comment:
you can submit the user_id within your job form, that should submit the user_id to the model