John Cowan
John Cowan

Reputation: 1674

Unkown Action - The action could not be found for Controller

I've read a few similar questions/answers but cannot find any answers to my bug.

I have a form for entering hours for a job and one for editing an hours record. Both the add and edit share a partial as per Rails guidelines. My add (new) form works fine. When submitting the edit form, I get the error/bug:

Unknown Action
The action '11' could not be found for HoursController

I get no other output on this screen.

The url shown for these screens are:

http://localhost:3000/hours/11/edit

after submit:

http://localhost:3000/hours/11

The form tag for my edit form is:

<%= form_for(:hour, :url => {:action => 'update', :id => @hour.id}) do |f| %>

This should go to the 'hours' controller, to the 'update' action. Maybe there is something else wrong with my controller, but the add/new form works fine... confusing...

My Controller:

class HoursController < ApplicationController

  before_filter :confirm_logged_in

  def index
  end

  def list
    if !params[:job_id].nil?
      @hours = Hour.where(["job_id = ?",params[:job_id]]).date_sorted
    else
      @hours = Hour.date_sorted
    end
  end

  def show
    @hour = Hour.find(params[:id])
  end

  def new
    @hour = Hour.new
  end

  def create
    @hour = Hour.new(params[:hours])
    if @hour.save
      job = Job.find(params[:hours][:job_id])
      flash[:notice] = "You have just entered hours for #{job.name}"
      redirect_to(:action => "list", :job_id => params[:hours][:job_id])
    else
      flash[:notice] = "There were one or more problems with your form. Please try again!"
      render('new')
    end
  end

  def edit
    @hour = Hour.find(params[:id])
  end

  def update
    @hour=Hour.find(params[:id])
    if @hour.update_attributes(params[:hour])
      flash[:notice] = "You have just updated an Hours entry"
      redirect_to(:action => "list", :job_id => params[:hours][:job_id])
    else
      render("edit")
    end
  end

  def delete
     @hour = Hour.find(id)
  end

  def destroy
    Hour.find(params[:id]).destroy
    flash[:notice] = "Hours Deleted Successfully!"
    redirect_to(:action => 'list')
  end

  def search
    if params[:job][:id]
      @job = Job.find(params[:job][:id])
      redirect_to(:action => "list", :job_id => @job.id)
    end
  end

end

My Routes file for the Hours section

resources :hours do
    collection do
      get :list
      get :delete
    end
end

Which gives me:

list_hours GET    /hours/list(.:format)                  hours#list
     delete_hours GET    /hours/delete(.:format)                hours#delete
            hours GET    /hours(.:format)                       hours#index
                  POST   /hours(.:format)                       hours#create
         new_hour GET    /hours/new(.:format)                   hours#new
        edit_hour GET    /hours/:id/edit(.:format)              hours#edit
             hour GET    /hours/:id(.:format)                   hours#show
                  PUT    /hours/:id(.:format)                   hours#update
                  DELETE /hours/:id(.:format)                   hours#destroy

Any ideas in how to figure this out would be greatly appreciated. --jc

Upvotes: 1

Views: 3312

Answers (2)

stevenspiel
stevenspiel

Reputation: 5999

My situation was slightly different because I had a custom route, but it may help someone else (or future me).

I kept getting that error until I added the method: :post to the form params.

routes:

resources :hours do
  member do
    post :fix
  end
end

html.erb

form_for @hour, url: fix_hour_path(@hour), method: :post do
  ...
end

Upvotes: 0

Raj Adroit
Raj Adroit

Reputation: 3888

Try like this

 <% form_for @hours do |f| %>

or else

<% form_for :hours, :url => 
     url_for(:action => "update", :id => @hours) do |f| %>

Upvotes: 1

Related Questions