Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1ruby-on-rails-3.2routes

Routing error that makes no sense...to me anyway


Ok here is this generic error message being displayed in the browser I'm receiving when I click a particular link:

No route matches {:action=>"edit", :controller=>"timesheets", :incident_id=>nil}

Here is the code for the page that contains the link (including the path to create the new record)

<div style="float:left;padding:25px;">
<p><%= link_to "Add a Command Officer", new_incident_timesheet_command_officer_path %></p>
</div>

Before I click the link the browser url reads as follows:

localhost:3000/incidents/197/timesheet/edit

And the above browser url makes total sense.

When I hover my mouse over the link to "Add a Command Officer" it says at the bottom of the browser: localhost:3000/incidents/197/timesheets/command_officers/new

But when I click it I get the routing error I mentioned at the top. Here is the code from the Command_Officers_controller and the routing file.

I believe I've included all the required info for someone to help me out but if I'm missing something let me know. Thanks for the help

Routes.rb

root :to => "incidents#index"

resources :users
resources :profiles

resources :incidents do

resource :mat_list
member do
post :send_notice
end
member do
get :changestatus
end



resource :timesheet do
    resources :command_officers
    resources :fire_chiefs
    resources :fire_fighters
    resources :safety_officers
    resources :emts
    resources :hazmat_specialists
    resources :command_vehicles
    resources :engines
    resources :emergency_supports
    resources :hazmat_units
    resources :field_units
    resources :pumpers
    resources :tankers
    resources :rescue_units
    resources :ladder_truck75s
    resources :ladder_truck150s
end
end


resource :session do
  member do
  get :resetpass
  post :resetpass
 end
end

command_officers_controller.rb

def new
   @command_officer = CommandOfficer.new
   @timesheet = Incident.find(params[:incident_id]).timesheet

   respond_to do |format|
     format.html # new.html.erb
   end
end

# GET /command_officers/1/edit
def edit
   @command_officer = CommandOfficer.find(params[:id])
end

# POST /command_officers
# POST /command_officers.xml
def create
   # @new_command_officer = CommandOfficer.new(params[:command_officer])
  @timesheet = Incident.find(params[:incident_id]).timesheet
      @command_officer = @timesheet.command_officer.build(params[:command_officer])

  respond_to do |format|
    if @command_officer.save
               format.html { redirect_to(edit_incident_timesheet_path, :notice => 'Command officer was successfully created.') }
    else
        flash[:error] = "What happened?"
        format.html { render :new }
        end
end
 end

Solution

  • The problem is that you are trying to generate some link on that next page, but the incident_id you are passing to rails is nil. Check your view/controller and remove/fix the offending code.

    More generally, learn how to read the stack trace that rails gives you so you can track these down yourself.