Pergunta

I get a NameError uninitialized constant WorkoutLog when attempting to access a non-resourceful action on a controller.

My controller looks as follows:

class WorkoutLogController < ApplicationController
   def index
     # random code
   end

   def on_date
     # random code
   end
end

My Routes File:

 match "workout_log/:date", :controller => "workout_log", :action => "on_date", :as => "log_on_date"
 match "workout_log", :controller => 'workout_log', :action => 'index'

And then I have a my link_to as such:

<%= link_to "View Log", log_on_date_path(:date => Date.today.strftime('%d-%B-%Y')), :remote => true, "data-type" => "html" %>

The WorkoutLogController has no model behind it - it's just a controller. However, I can't perform a request on the on_date because it throws the following error:

NameError (uninitialized constant WorkoutLog):

When I run rake routes it seems to be fine as it generates the following:

log_on_date        /workout_log/:date(.:format)                  {:controller=>"workout_log", :action=>"on_date"}
workout_log        /workout_log(.:format)                        {:controller=>"workout_log", :action=>"index"}

I can't wrap my head around what the issue is (especially after spending the past night trying to figure it out). Is rails looking for a model to associate with it and failing to do so?

Foi útil?

Solução

I figured out the issue. It had to do with declarative_authorization.
Since I had not copied the code 100%, I left out the following from my controller code pasted above:

filter_resource_access

declarative_authorization uses this for resourceful routes - which was not my case. I've since switched it to filter_access_to :all and it works fine. I got to the root of the problem by creating the file workout_log.rb in my models:

class WorkoutLog
end

Then when I issued a request to the index and on_date actions, it gave me the error that it could not find ActiveModel methods on WorkoutLog, which in turn pointed me to the filters on the WorkoutLogController.

If you look under Examples > Controller at the declarative_authorization page on GitHub, it'll provide the necessary info. Hope this helps anyone else who has this issue!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top