I'm struggling to get nested attributes down. Working off of Railscast 196, I tried to setup my own app that does basic nesting. Users can create scavenger hunts. Each hunt consists of a series of tasks (that can belong to any hunt, not just one). I got a little help here and tried to learn from a post with a similar issue, but I'm still stuck. I've been hacking around for hours and I've hit a brick wall.
class HuntsController < ApplicationController
def index
#title = "All Hunts"
#hunts = Hunt.paginate(:page => params[:page])
end
def show
#hunt = Hunt.find(params[:id])
#title = #hunt.name
#tasks = #hunst.tasks.paginate(:page => params[:page])
end
def new
if current_user?(nil) then
redirect_to signin_path
else
#hunt = Hunt.new
#title = "New Hunt"
3.times do
#hunt = #hunt.tasks.build
#hunt = #hunt.hunt_tasks.build
hunt = #hunt.hunt_tasks.build.build_task
end
end
end
def create
#hunt = Hunt.new(params[:hunt])
if #hunt.save
flash[:success] = "Hunt created!"
redirect_to hunts_path
else
#title = "New Hunt"
render 'new'
end
end
....
end
With this code, when I try and create a new hunt, I'm told that there's no method "build_task" (it's undefined). So when I remove that line and use the second bit of code that's commented out above, I get the error below.
NoMethodError in Hunts#new
Showing /Users/bendowney/Sites/MyChi/app/views/shared/_error_messages.html.erb where line #1 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.errors
Extracted source (around line #1):
1: <% if object.errors.any? %>
2: <div id="error_explanation">
3: <h2><%= pluralize(object.errors.count, "error") %>
4: prohibited this <%= object.class.to_s.underscore.humanize.downcase %>
Trace of template inclusion: app/views/tasks/_fields.html.erb, app/views/hunts/_fields.html.erb, app/views/hunts/new.html.erb
And when I use the first bit of code that's commented out in the hunt controller, then I get an error telling me that my 'new' method has an unintialized constant:
NameError in HuntsController#new
uninitialized constant Hunt::Tasks
I'm at my wit's end. Any suggestions on what exactly I'm doing wrong? Or a strategy Here are my models:
class Hunt < ActiveRecord::Base
has_many :hunt_tasks
has_many :tasks, :through => :hunt_tasks #, :foreign_key => :hunt_id
attr_accessible :name
validates :name, :presence => true,
:length => { :maximum => 50 } ,
:uniqueness => { :case_sensitive => false }
end
class Task < ActiveRecord::Base
has_many :hunt_tasks
has_many :hunts, :through => :hunt_tasks#, :foreign_key => :hunt_id
attr_accessible :name
validates :name, :presence => true,
:length => { :maximum => 50 } ,
:uniqueness => { :case_sensitive => false }
end
class HuntTask < ActiveRecord::Base
belongs_to :hunts # the id for the association is in this table
belongs_to :tasks
end
When you create an association between 2 of your models, you add functionality to them, depending on how you define your relationship. Each type kinda adds different functions to your model.
I really recommend reading this guide -> http://guides.rubyonrails.org/association_basics.html
Here you can see which functions get added by each different type of association.
http://guides.rubyonrails.org/association_basics.html#detailed-association-reference
If I do a small sample program like...
class HuntsController < ApplicationController
# GET /hunts
# GET /hunts.json
def index
#hunts = Hunt.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #hunts }
end
end
# GET /hunts/1
# GET /hunts/1.json
def show
#hunt = Hunt.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #hunt }
end
end
# GET /hunts/new
# GET /hunts/new.json
def new
#hunt = Hunt.new
3.times do |i|
t = #hunt.hunt_tasks.build
t.name = "task-#{i}"
end
respond_to do |format|
format.html # new.html.erb
format.json { render json: #hunt }
end
end
# GET /hunts/1/edit
def edit
#hunt = Hunt.find(params[:id])
end
# POST /hunts
# POST /hunts.json
def create
#hunt = Hunt.new(params[:hunt])
respond_to do |format|
if #hunt.save
format.html { redirect_to #hunt, notice: 'Hunt was successfully created.' }
format.json { render json: #hunt, status: :created, location: #hunt }
else
format.html { render action: "new" }
format.json { render json: #hunt.errors, status: :unprocessable_entity }
end
end
end
# PUT /hunts/1
# PUT /hunts/1.json
def update
#hunt = Hunt.find(params[:id])
respond_to do |format|
if #hunt.update_attributes(params[:hunt])
format.html { redirect_to #hunt, notice: 'Hunt was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #hunt.errors, status: :unprocessable_entity }
end
end
end
# DELETE /hunts/1
# DELETE /hunts/1.json
def destroy
#hunt = Hunt.find(params[:id])
#hunt.destroy
respond_to do |format|
format.html { redirect_to hunts_url }
format.json { head :no_content }
end
end
end
and this model-relation
class Hunt < ActiveRecord::Base
has_many :hunt_tasks
end
class HuntTask < ActiveRecord::Base
belongs_to :hunt
end
and add this snippet somewhere in views/hunts/_form.html
<% #hunt.hunt_tasks.each do |t| %>
<li><%= t.name %></li>
<% end %>
I get regular output, seeing that the 3 tasks were created.
have you tried
hunttask = #hunt.build_hunt_task
in the HuntsController new action?
http://guides.rubyonrails.org/association_basics.html#detailed-association-reference
The immediate error you are seeing is in app/views/shared/_error_messages.html.erb. object is not defined, You probably need to find where that partial is called. Find:
render :partial=>"/shared/error"
replace it with
render :partial=>"/shared/error", :locals=>{:object=>#hunt}
If you find it in app/views/hunts somewhere, if you find in in app/views/tasks, replace #hunt with #task
That will at least show you what the real error is.
Related
In rails console & using the models below, I connected grades K, 1, and 2 to the school whose Edit form has this select field:
As you can see, that association correctly selects the 3 items in the field, but if I click to select/deselect grades, those changes aren't getting saved.
Here are the models:
# app/models/school.rb
class School < ActiveRecord::Base
has_many :grades_schools, inverse_of: :school
has_many :grades, through: :grades_schools
accepts_nested_attributes_for :grades_schools, allow_destroy: true
end
# app/models/grades_school.rb
class GradesSchool < ActiveRecord::Base
belongs_to :school
belongs_to :grade
end
# app/models/grade.rb
class Grade < ActiveRecord::Base
has_many :grades_schools, inverse_of: :grade
has_many :schools, through: :grades_schools
end
The form looks like this:
# app/views/schools/_form.html.haml
= form_for(#school) do |f|
/ <snip> other fields
= collection_select(:school, :grade_ids, #all_grades, :id, :name, {:selected => #school.grade_ids, include_hidden: false}, {:multiple => true})
/ <snip> other fields + submit button
And the controller looks like this:
# app/controllers/schools_controller.rb
class SchoolsController < ApplicationController
before_action :set_school, only: [:show, :edit, :update]
def index
#schools = School.all
end
def show
end
def new
#school = School.new
#all_grades = Grade.all
#grades_schools = #school.grades_schools.build
end
def edit
#all_grades = Grade.all
#grades_schools = #school.grades_schools.build
end
def create
#school = School.new(school_params)
respond_to do |format|
if #school.save
format.html { redirect_to #school, notice: 'School was successfully created.' }
format.json { render :show, status: :created, location: #school }
else
format.html { render :new }
format.json { render json: #school.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #school.update(school_params)
format.html { redirect_to #school, notice: 'School was successfully updated.' }
format.json { render :show, status: :ok, location: #school }
else
format.html { render :edit }
format.json { render json: #school.errors, status: :unprocessable_entity }
end
end
end
private
def set_school
#school = School.find(params[:id])
end
def school_params
params.require(:school).permit(:name, :date, :school_id, grades_attributes: [:id])
end
end
I have a feeling that the crux of my problem has to do with a mismatch between the params generated by collection_select and the strong parameters. One or both of these is probably incorrect, but I can't for the life of me find example code online that shows me what I'm doing wrong.
After trying a load of failed variations, I'm at my wits end! Thanks in advance for your help!
Crap. I could have sworn I tried this before, but it must have been when using fields_for in the form instead of collection_select. The solution:
def school_params
params.require(:school).permit(:name, :date, :school_id, grades_attributes: [:id])
end
becomes
def school_params
params.require(:school).permit(:name, :date, :school_id, grade_ids: [])
end
I'm still curious how it would work when using fields_for #grades_schools, but will have to save that investigation for another day....
Newbie alert! I hope I can explain my situation accurately and understandably.
I have a model called animals. Another model called statuses. Another called sexes.
From animal.rb model:
class Animal < ActiveRecord::Base
belongs_to :status, :class_name => Status, :foreign_key => :status_id
belongs_to :sex, :class_name => Sex, :foreign_key => :sex_id
end
sex.rb model:
class Sex < ActiveRecord::Base
has_many :animals
end
status.rb model:
class Status < ActiveRecord::Base
has_many :animals
end
In my views/animals/index.html.erb I have this:
<% #animals.each do |animal| %>
<tr>
<td><%= animal.name %></td>
<td><%= animal.eartag %></td>
<td><%= animal.sex.sex %></td>
<td><%= animal.status.status %></td>
...
The column in the table displays the sex from the sexes lookup table fine, but the line for status causes the following error:
NoMethodError in Animals#index
undefined method `status' for nil:NilClass
So I guess Rails is thinking animal.status is nil?? I don't know why it would be when animal.sex is not nil.
Also, I have views/animals/show.html.erb. Part of that is:
<dt><strong><%= model_class.human_attribute_name(:sex_id) %>:</strong></dt>
<dd><%= #animal.sex.sex %></dd>
<dt><strong><%= model_class.human_attribute_name(:status_id) %>:</strong></dt>
<dd><%= #animal.status.status %></dd>
And this works fine. I.e. in the columns for Sex and Status, it displays the values from the sex and status columns of the sex and status tables, respectively, as I want the index.html.erb view to do.
I don't know if it makes any difference, but here's animals_controller.rb:
class AnimalsController < ApplicationController
before_action :set_animal, only: [:show, :edit, :update, :destroy]
# GET /animals
# GET /animals.json
def index
#animals = Animal.all
end
# GET /animals/1
# GET /animals/1.json
def show
end
# GET /animals/new
def new
#animal = Animal.new
end
# GET /animals/1/edit
def edit
end
# POST /animals
# POST /animals.json
def create
#animal = Animal.new(animal_params)
respond_to do |format|
if #animal.save
format.html { redirect_to #animal, notice: 'Animal was successfully created.' }
format.json { render action: 'show', status: :created, location: #animal }
else
format.html { render action: 'new' }
format.json { render json: #animal.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /animals/1
# PATCH/PUT /animals/1.json
def update
respond_to do |format|
if #animal.update(animal_params)
format.html { redirect_to #animal, notice: 'Animal was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #animal.errors, status: :unprocessable_entity }
end
end
end
# DELETE /animals/1
# DELETE /animals/1.json
def destroy
#animal.destroy
respond_to do |format|
format.html { redirect_to animals_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_animal
#animal = Animal.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def animal_params
params.require(:animal).permit(:name, :eartag, :reg_num, :sex_id, :date_birth, :date_acquired, :date_removed, :status_id, :num_horns, :sire_id, :dam_id, :source_id, :origin_id, :percent_black, :percent_lilac, :for_sale, :for_sale_status_id, :quality_id, :sale_price, :to_be_culled, :comments, :comments_web, :show_on_website, :core_flock, :birth_id, :rejected_at_birth)
end
end
I hope this makes some kind of sense. Thanks!
Change this line
<%= animal.status.status %>
to
<%= animal.status.status if animal.status %>
This will prevent the error. Will output animal.status.status only if animal.status is present.
I'm a bit new in ruby/rails/POO and I'm a bit lost in a form that I'm realizing.
I'm using the gem formtastic and I'm doing it in haml.
I have this model
class Help < ActiveRecord::Base
attr_accessible :answer, :category, :question
validates :category, presence: true, uniqueness: true
validates :question, presence: true
validates :answer, presence: true
end
In my form, I want the possibility to create a new Question/Answer with its category.
The category should be selected in a selectbox but if the category I want is not listed yet, I want to have the ability to add it.
Here's the form
= semantic_form_for #help do |f|
= f.inputs do
= f.input :category, :as => :select, :collection => Help.category
= f.input :category
= f.input :question
= f.input :answer
= f.action :submit, :as => :button
EDIT :
class HelpsController < ApplicationController
# GET /helps
# GET /helps.json
def index
#helps = Help.all.sort_by {|f| f.category}
respond_to do |format|
format.html # index.html.erb
format.json { render json: #helps }
end
end
# GET /helps/1
# GET /helps/1.json
def show
#help = Help.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #help }
end
end
# GET /helps/new
# GET /helps/new.json
def new
#help = Help.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #help }
end
end
# GET /helps/1/edit
def edit
#help = Help.find(params[:id])
end
# POST /helps
# POST /helps.json
def create
#help = Help.new(params[:help])
respond_to do |format|
if #help.save
format.html { redirect_to #help, notice: 'Help was successfully created.' }
format.json { render json: #help, status: :created, location: #help }
else
format.html { render action: "new" }
format.json { render json: #help.errors, status: :unprocessable_entity }
end
end
end
# PUT /helps/1
# PUT /helps/1.json
def update
#help = Help.find(params[:id])
respond_to do |format|
if #help.update_attributes(params[:help])
format.html { redirect_to #help, notice: 'Help was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #help.errors, status: :unprocessable_entity }
end
end
end
# DELETE /helps/1
# DELETE /helps/1.json
def destroy
#help = Help.find(params[:id])
#help.destroy
respond_to do |format|
format.html { redirect_to helps_url }
format.json { head :no_content }
end
end
end
When I try to reach /help/new it actually says to me :
undefined method `model_name' for NilClass:Class
The aim is to have in the selectbox, categories already registered, and if the user is not founding the category he wants to use in the selectbox, he can create one by typing it in the input.
Any clues to help me doing this ?
Cordially ,
Rob
Try this:
= f.collection_select :category
I found a method that does half what i wanted.
It's the method pluck.
I defined a static method in my model :
def self.getcat
Help.pluck(:category)
end
Then in my form in simply call this method on my collection :
= f.input :category, :as => :select, :collection => Help.getcat
I've had a problem over the last few days getting my nested resources to create and display properly. There are tons of similar questions on StackOverflow and lots of blog posts on this, but they all seem to either deal with a older version of Rails or a different issue. I'm at the point where once I finally fix something, another error pops up. I've narrowed it down to me making a stupid mistake or typo being too inexperienced to notice.
I have a Jobs model that belongs to a Venue model. The venue works fine and I've even gotten as far as to be able to go to my nested Jobs index under each Venue and bring up the New and Edit forms, but going to 'Show' or creating a new Job caused an undefined method error. After a lot of searching I found plenty with the same problem and tried to implement their fixes, but now I'm getting a Routing Error.
Most of my confusing comes from when to leave off the #, when to use :venue_id instead of :id in params, etc. Every example I see seems to have a different way and I can't seem to make any of them work for me.
Any bump in the right direction would be extremely helpful.
The Routing Error
No route matches {:action=>"show", :controller=>"jobs", :venue_id=>#<Venue id: 1, name: "Burger Chef", city: "Chicago", state: "Illinois", areacode: 60614, created_at: "2013-02-05 21:33:41", updated_at: "2013-02-06 23:01:05", avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>}
routes.rb
Twist::Application.routes.draw do
resources :users
devise_for :users
resources :venues do
resources :jobs
end
end
jobs_controller.rb
class JobsController < ApplicationController
# GET /jobs
# GET /jobs.json
before_filter :get_venue
def get_venue
#venue = Venue.find(params[:venue_id])
end
def index
#jobs = #venue.jobs
respond_to do |format|
format.html # index.html.erb
format.json { render json: #jobs }
end
end
# GET /jobs/1
# GET /jobs/1.json
def show
#job = #venue.job.find(params[:id])
if params[:id]
#venue = Venue.where(:id => params[:id]).first
#jobs = #venue.job_url
else
#jobs = Jobs.all
end
respond_to do |format|
format.html # show.html.erb
format.json { render json: #job }
end
end
# GET /jobs/new
# GET /jobs/new.json
def new
#job = #venue.jobs.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #job }
end
end
# GET /jobs/1/edit
def edit
#job = #venue.job.find(params[:venue_id])
end
# POST /jobs
# POST /jobs.json
def create
#job = #venue.jobs.new(params[:job])
respond_to do |format|
if #job.save
format.html { redirect_to :action => :show, :id => #venue.id,
notice: 'Job was successfully created.' }
format.json { render json: [#venue, #job],
status: :created,
location: [#venue, #job] }
else
format.html { render action: "new" }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
# PUT /jobs/1
# PUT /jobs/1.json
def update
#job = Job.find(params[:id])
respond_to do |format|
if #job.update_attributes(params[:job])
format.html { redirect_to #job, notice: 'Job was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
# DELETE /jobs/1
# DELETE /jobs/1.json
def destroy
#job = Job.find(params[:id])
#job.destroy
respond_to do |format|
format.html { redirect_to jobs_url }
format.json { head :no_content }
end
end
end
venues_controller.rb
class VenuesController < ApplicationController
# GET /venues
# GET /venues.json
def index
#venues = Venue.all
if params[:name]
#user = User.where(:name => params[:name]).first
#venues = #user.venues
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #venues }
end
end
# GET /venues/1
# GET /venues/1.json
def show
#venue = Venue.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #venue }
end
end
# GET /venues/new
# GET /venues/new.json
def new
#venue = Venue.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #venue }
end
end
# GET /venues/1/edit
def edit
#venue = Venue.find(params[:id])
#if session[:user_id] != #venue.user_id
# flash[:notice] = "Sorry, you cannot edit this venue."
# redirect_to(venues_path)
# =>end
end
# POST /venues
# POST /venues.json
def create
#venue = Venue.new(params[:venue_id])
respond_to do |format|
if #venue.save
format.html { redirect_to #venue, notice: 'Venue was successfully created.' }
format.json { render json: #venue, status: :created, location: #venue }
else
format.html { render action: "new" }
format.json { render json: #venue.errors, status: :unprocessable_entity }
end
end
end
# PUT /venues/1
# PUT /venues/1.json
def update
#venue = Venue.find(params[:id])
respond_to do |format|
if #venue.update_attributes(params[:venue])
format.html { redirect_to #venue, notice: 'Venue was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #venue.errors, status: :unprocessable_entity }
end
end
end
# DELETE /venues/1
# DELETE /venues/1.json
def destroy
#venue = Venue.find(params[:id])
#venue.destroy
respond_to do |format|
format.html { redirect_to venues_url }
format.json { head :no_content }
end
end
end
job.rb
class Job < ActiveRecord::Base
attr_accessible :description, :name, :requirement, :venue_id
validates :name, :presence => true, :length => { :minimum => 3 }
belongs_to :venue
end
venue.rb
class Venue < ActiveRecord::Base
attr_accessible :areacode, :avatar, :city, :name, :state
has_many :jobs
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
/jobs/show.html.erb
<p id="notice"><%= notice %></p>
<% #job = Job.find(param[:venue_id]) %>
<p>
<b>Name:</b>
<%= #job.name %>
</p>
<p>
<b>Company:</b>
<p><%= #venue.name %></p>
<p><%= link_to #job.venue.name, venue_path(#venue) %></p>
<p>
<b>Job:</b>
<%= #job.job_id %>
</p>
<p>
<b>Description:</b>
<%= #job.description %>
</p>
<p>
<b>Requirement:</b>
<%= #job.requirement %>
</p>
<%= link_to 'Edit', edit_venue_job_path(#venue, #job) %> |
<%= link_to 'Back', venue_jobs_path(#venue, #job) %>
**/jobs/index.html.erb**
<div class="usergrid">
<% jobs = #venue.jobs %>
<% #venue.jobs.each do |job| %>
<div class = "user venue">
<p>
<h2><%= link_to job.name, venue_job_path(#venue) %></h2>
<%= link_to 'Edit', edit_venue_job_path(#venue, job) %><br/>
<%= link_to 'Delete', venue_jobs_path(#venue, #job), :confirm => 'Are you sure?', :method => :delete %>
</div>
<% end %>
<div style="clear:both"></div>
</div>
<%= link_to 'New Job', new_venue_job_path(#venue) %>
I realize that...
This might be a really obvious fix, but being new to Rails there are definitely still some fundamentals that I don't completely understand.
If I posted too much or too little code please let me know, I'm not entirely sure how much is the least necessary for this.
There might be more than one error, there might be a lot of errors, and many of them could have been caused by me trying to fix this when I really didn't know what I was doing. I wanted to fix this myself, but I can't handle messing with it anymore when I'm really just making it worse.
I've tried messing with the routes, changing the actual links and routes, messing with scope, and as many of the common fixes for these errors that I could find and none of them seemed to help.
Thanks!
The error is saying that there is no route for the given params :
{:action=>"show", :controller=>"jobs", :venue_id=> "an_id"}
You can check that by running rake routes, and you'll see that, as jobs is nested under venue, the jobs#show controller actions needs two parameters : the venue_id (which is the job's 'parent') and the id which is the job id.
I quickly checked your code, and I think that one one of the things that causes the error is this line :
<h2><%= link_to job.name, venue_job_path(#venue) %></h2>
this should be
<h2><%= link_to job.name, venue_job_path(#venue, #job) %></h2>
Basically you'll get that kind of error, whenever you'll try to render a link to a job without providing the venue.
Let me know if you need more details or more information.
I have a nested form with the following models:
class Incident < ActiveRecord::Base
has_many :incident_notes
belongs_to :customer
belongs_to :user
has_one :incident_status
accepts_nested_attributes_for :incident_notes, :allow_destroy => false
end
class IncidentNote < ActiveRecord::Base
belongs_to :incident
belongs_to :user
end
Here is the controller for creating a new Incident.
def new
#incident = Incident.new
#users = #customer.users
#statuses = IncidentStatus.find(:all)
#incident.incident_notes.build(:user_id => current_user.id)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #incident }
end
end
def create
#incident = #customer.incidents.build(params[:incident])
#incident.incident_notes.build(:user_id => current_user.id)
respond_to do |format|
if #incident.save
flash[:notice] = 'Incident was successfully created.'
format.html { redirect_to(#incident) }
format.xml { render :xml => #incident, :status => :created, :location => #incident }
else
format.html { render :action => "new" }
format.xml { render :xml => #incident.errors, :status => :unprocessable_entity }
end
end
end
This all exists in a nested form for an incident. There is a text area for the incident_notes form that is nested in the incident.
So my problem is that the incident_notes entry is submitted twice whenever I create the incident. The first insert statement creates an incident_note entry with the text from the text area, but it doesn't attach the user_id of the user as the foreign key. The second entry does not contain the text, but it has the user_id.
I thought I could do this with:
#incident.incident_notes.build(:user_id => current_user.id)
but that does not appear to work the way I want. How can I attach the user_id to incident_note?
Thanks!
I finally figured it out. I needed to do this in the Incident controller:
def create
#incident = #customer.incidents.build(params[:incident])
#incident.incident_notes.first.user = current_user
rather than:
def create
#incident = #customer.incidents.build(params[:incident])
#incident.incident_notes.build(:user_id => current_user.id)
I don't think you need
#incident.incident_notes.build(:user_id => current_user.id)
on new action. You're building the incident_notes twice.