NoMethodError in NameController#new - ruby-on-rails-3

I've run into an error in my application, to explain the context: a user has several cars, each car can make a tanking therefore my TankingLogsController is the following
class TankingLogsController < ApplicationController
def new
#user = User.find(params[:user_id])
#car = #user.cars.find(params[:car_id])
#tankinglog = #cars.tanking_logs.build
end
end
This is my routes.rb file
Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :users
resources :users do
resources :cars do
resources :tanking_logs
end
end
...
I encountered this error:
NoMethodError in TankingLogsController#new
undefined method `tanking_logs' for nil:NilClass
Application Trace | Framework Trace | Full Trace
app/controllers/tanking_logs_controller.rb:5:in `new'
Why is my application failing?

You have a very obvious error, and Ruby is telling you exactly what it is and where to find it.
You've written:
#car = #user.cars.find(params[:car_id])
#tankinglog = #cars.tanking_logs.build
You initialize a variable called #car on the first line, and you reference a second uninitialized variable #cars on the second line.
You have never initialized the #cars variable, so it is nil. Calling #cars.tanking_logs causes undefined method 'tanking_logs' for nil:NilClass, because you're effectively writing nil.tanking_logs.
The second line should be using #car.tanking_logs.

Related

SystemStackError - stack level too deep with a User Search

when searching for a User I end up getting a SystemStackError.
I've copied this over from another project I did which works fine, so I'm struggling to work out why it isn't working.
The suspect code:
class ProfilesController < ApplicationController
before_filter :find_user
def show
if #user
render action: show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
private
def find_user
#user = User.find_by_profile_name(params[:id])
end
end
And the error:
Started GET "/Benji" for 127.0.0.1 at 2013-12-07 18:23:57 +0000
ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ProfilesController#show as HTML
Parameters: {"id"=>"Benji"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."profile_name" = 'Benji' LIMIT 1
Completed 500 Internal Server Error in 111ms
SystemStackError - stack level too deep:
actionpack (4.0.2) lib/action_dispatch/middleware/reloader.rb:70:in `'
Thanks
It should be
render action: "show"
not
render action: show
In your code, show is a method call and it references itself causing a recursive infinite loop.
You can simplify your code even more
class ProfilesController < ApplicationController
before_filter :find_user
def show
end
private
def find_user
unless #user = User.find_by_profile_name(params[:id])
render file: 'public/404', status: 404, formats: [:html]
end
end
end
I believe the render issue definitely answers the question.
Also worth noting - in your find_user before filter, the Rails find_by_* method you are calling is searching via the profile name attribute of your model. However, you are passing in params[:id] to the method. Just wanted to make sure that was correct.

uninitialized constant ProfilesController

I have an error "uninitialized constant ProfilesController" on my Profiles controller. This is the profiles_controller.rb:
class ProfilesController < ApplicationController
def new
#profile = Profile.new
end
def create
#profile = Profile.new(params[:profile])
if #profile.save
redirect_to profile_path, notice: I18n.t('.profile.created')
else
render action: "new"
end
end
end
This is the routes.rb:
resources :profiles, only: [:new, :create]
And this the output of rake routes:
profiles POST /profiles(.:format) profiles#create
new_profile GET /profiles/new(.:format) profiles#new
When I click a link for "new_profile_path" I get the error, but to me everything seems OK? The controller name is plural,the routes are OK?
You most likely spelled your controller file wrong. Confirm that file is truly: `/app/controllers/profiles_controller.rb'
Really strange, I created a Books controller with a generator, renamed everything to Profiles and then it works as normal. As far as I can see the routes are identical. Strange....
I had the same problem when i checked the name of the controller was 'profile_controller.rb'(i had created it manually though). but inside the definition it was "ProfilesController".
class ProfilesController < ApplicationController
def index
end
def new
#profile = Profile.new
end
def create
end
end
So if your controller name is correct and you have added the route("resources :profiles") then it will work as expected

form_for - error accessing a controller variable in the view (rails 3)

I have a very simple model/view but for some reason I can't seem to access the new record variable and therefore get the error:
undefined method `hash_for_checklists_path' for # Module:<0x00000100f8b9b0>
I've hit my head against a wall on this for several hours. Can anyone see what I'm doing wrong?
Controller
class ChecklistsController < ApplicationController
def new
#title = "New Checklist"
#checklist = Checklist.new
end
[...]
end
(incidentally my application.html.erb file has no problem getting the #title variable.)
View (new.html.erb)
<%= form_for #checklist do |f| -%>
Routes.rb
devise_for :users
resources :checklist_item_categories, :as => 'item_categories' do
resources :checklist_items
end
resources :checklist_categories do
resources :checklists
end
match 'checklists/new', :to => 'checklists#new'
#pages
get "pages/home"
get "pages/contact"
#checklist items
get "checklist_items/new"
#checklists
get "checklists/new"
get "checklists/edit"
get "checklists/show"
get "checklists/index"
#categories
get "abstract_categories/new"
You don't have a path for the post from the form.
Maybe put
resources :checklists
in place of
#checklists
get "checklists/new"
get "checklists/edit"
...

Posting with Nested Resources

I have nested my resources (see below) and when I try to create a new entity, I get the following error. Does anyone know why I'm getting this error and how to solve it?
undefined method `applications' for nil:NilClass
resources careers do
resources applications
end
Within the 'Applications' controller I have:
before_filter [[:authenticate, :except => :new], :load_career]
def create
# The following line is where the error originates
#application = #career.applications.new(params[:application])
respond_to do |format|
...
end
end
private
def load_career
#career = Career.find(params[:career_id])
end
The Career and Application models have has_many :applications and belongs_to :career respectively.
And the '*_create_applications' migration has a career_id field.
I have never seen before_filters defined that way. I just tried it in Rails 3 and it doesn't seem to do anything. I would give each callback it's own before_filter call:
before_filter :authenticate, :except => :new
before_filter :load_career

NoMethodError - undefined method

I'm having problems displaying data from a separate controller. I have a number of users, each with many pages. I've followed this tutorial with a few minor adjustments.
The error that keeps appearing is:
NoMethodError in SitesController#show
undefined method `page' for #<ActionDispatch::Request:0x00000102452d30>
My routes.rb is as follows:
devise_for :users
resources :users, :only => [:index, :show] do
resources :pages, :shallow => true
end
match '/' => 'sites#show', :constraints => { :subdomain => /.+/ }
root :to => "home#index"
And I have a sites controller:
class SitesController < ApplicationController
def show
#site = Site.find_by_name!(request.page)
end
end
I've also tried:
def show
#site = Site.find_by_name!(params[:site])
end
Which gives a different error.
Am totally stuck trying to figure this out!
Looking forward to your assistance.
Bob
The problem is here: request.page
The request object is of the class ActionDispatch::Request, which does not have a page method.
To track down errors like this, you can try either looking at the docs or messing around in the debugger.
Try running your controller with --debugger enabled.
If you are running Ruby 1.8, install the ruby-debug gem.
If you are running Ruby 1.9, install the ruby-debug19 gem.
Add a debugger call here:
class SitesController < ApplicationController
def show
debugger
#site = Site.find_by_name!(request.page)
end
end
Run your server with the --debugger option.
See what p request.page does. I bet it will have an "undefined method" error, just you see when you try to view that controller action.
If you do a p request.class you can find out what class the object is, and then look up the docs to see how to use it.