Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails Class Variable not being referenced by controller
    primarykey
    data
    text
    <p>I have a small web app in Ruby on Rails which lists antique documents and the locations that are referenced by the documents. I can update locations that are already attached to documents, but when trying to create a new document, I get the following error</p> <pre><code>undefined method `locations' for nil:NilClass </code></pre> <p>The NilClass occurs in LocationController#new:</p> <pre><code>def new @location = @document.locations.new end </code></pre> <p>This is called from the view as such</p> <pre><code>&lt;%= form_for(@location,:url=&gt;new_document_location_path(@document)) do |f| %&gt; &lt;%= render :partial =&gt; "document_locations/form", :locals =&gt;{:f=&gt;f} %&gt; &lt;%= f.submit "Save",:class=&gt;"span2 btn" %&gt; &lt;%end%&gt; </code></pre> <p>So for some reason, the @document class isn't being referenced by the controller. I know the class is accessible to the view and in scope as I am able to view document attributes within the form (eg &lt;%= @document.id %></p> <p>So, why is the Controller not able to reference the @document variable? I'm assuming this is down to how I am passing the class variable. Eitherway, I'd be very grateful for some pointers.</p> <p>This is how the models are defined </p> <pre><code>class Document &lt; ActiveRecord::Base has_many :locations, :dependent =&gt; :destroy end class Location &lt; ActiveRecord::Base belongs_to :document end </code></pre> <p>Here's my routes</p> <pre><code>Pastpaper::Application.routes.draw do # added in to test match '/documents/:id/locations/create' =&gt; 'locations#create' resources :documents do collection do match 'permanent_delete/:id' =&gt; 'documents#permanently_delete',:as =&gt; :permanent_delete end match '/authorinfo' =&gt; 'documents#authorinfo',:as =&gt; :authorinfo match '/publicationinfo' =&gt; 'documents#publicationinfo',:as =&gt; :publicationinfo match '/images' =&gt; 'documents#itemimages' ,:as =&gt; :itemimages match '/locations' =&gt; 'documents#locations',:as =&gt; :locations match '/itempeople' =&gt; 'documents#itempeople' ,:as =&gt; :itempeople match '/people_facts_locations' =&gt; 'documents#people_facts_locations',:as =&gt; :people_facts_loc resources :locations, :controller=&gt;"locations" resources :people ,:controller =&gt; "document_people" resources :document_facts resource :facts resources :document_photos end resources :home do collection do get 'document_search','simple_location_search','date_search','simple_people_search','simple_organisation_search','document_filter' get 'search_results' post 'search_results' end end match 'documents/publicationinfo/:id' =&gt; 'documents#publicationinfo',:as =&gt; :publicationinfo match 'documents/update/publishinginfo/:id' =&gt; 'documents#publishinginfo',:as =&gt; :publishinginfo match 'documents/document_image_remove/:id' =&gt; 'documents#remove_image',:as=&gt;"remove_image" match 'documents/make_primary_image/:id' =&gt; 'documents#make_primary_image',:as =&gt; :make_primary_image match 'person_detail/:id' =&gt; 'documents#person_detail',:as=&gt;'person_detail' match 'about', :to=&gt; 'pages#about' match 'contact', :to =&gt; 'pages#contact' match 'privacy', :to =&gt; 'pages#privacy' match 'terms', :to =&gt; 'pages#terms' match 'help', :to =&gt; 'pages#help' namespace :admin do resources :document_types resources :statuses resources :attribute_types resources :event_types resources :users resources :orders resources :documents match 'restore_document/:id' =&gt; 'Documents#restore_document', :as =&gt; 'restore_document' match 'report' =&gt; 'report#index' ,:as=&gt;:report match 'report/surname_report' =&gt; 'report#surname_report',:as=&gt;:surname_report match 'report/location_report' =&gt; 'report#location_report',:as=&gt;:location_report end root :to =&gt; 'home#index' end </code></pre> <p>And here's the relevant Controllers: DocumentController</p> <pre><code>class DocumentsController &lt; ApplicationController before_filter :prepare_document ,:only =&gt; [:locations] def locations @locations = @document.locations.order("id asc") @location = @document.locations.new end private def prepare_document if params[:id] @document = Document.find(params[:id], :include =&gt; [:document_attributes]) elsif params[:document_id] @document = Document.find(params[:document_id], :include =&gt; [:document_attributes]) end end end </code></pre> <p>LocationsController</p> <pre><code>class LocationsController &lt; ApplicationController before_filter :prepare_document def new @location = @document.locations.new end def create @location = @document.locations.build if @location.save redirect_to document_locations_url(@document) else render "new" end end def update @location = @document.locations.find(params[:id]) if @location.update_attributes(params[:location]) redirect_to document_locations_url(@document) else render "edit" end end def prepare_document if params[:document_id] if current_user.is_admin? @document = Document.find(params[:document_id], :include =&gt; [:document_attributes]) else @document = current_user.documents.find(params[:document_id], :include =&gt; [:document_attributes]) end end end end </code></pre> <p>This is how my routes rake out</p> <pre><code> logout GET /logout(.:format) {:action=&gt;"destroy", :controller=&gt;"sessions"} login POST /login(.:format) {:action=&gt;"create", :controller=&gt;"sessions"} forgot_password /forgot_password(.:format) {:action=&gt;"new", :controller=&gt;"password_resets"} user_home GET /profile(.:format) {:action=&gt;"index", :controller=&gt;"users"} deactivateaccount /account/deactivate(.:format) {:controller=&gt;"users", :action=&gt;"accountdeactivate"} changepassword /account/changepassword(.:format) {:controller=&gt;"users", :action=&gt;"changepassword"} register /user/registration(.:format) {:controller=&gt;"users", :action=&gt;"new"} users GET /users(.:format) {:action=&gt;"index", :controller=&gt;"users"} POST /users(.:format) {:action=&gt;"create", :controller=&gt;"users"} new_user GET /users/new(.:format) {:action=&gt;"new", :controller=&gt;"users"} edit_user GET /users/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"users"} user GET /users/:id(.:format) {:action=&gt;"show", :controller=&gt;"users"} PUT /users/:id(.:format) {:action=&gt;"update", :controller=&gt;"users"} DELETE /users/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"users"} password_resets GET /password_resets(.:format) {:action=&gt;"index", :controller=&gt;"password_resets"} POST /password_resets(.:format) {:action=&gt;"create", :controller=&gt;"password_resets"} new_password_reset GET /password_resets/new(.:format) {:action=&gt;"new", :controller=&gt;"password_resets"} edit_password_reset GET /password_resets/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"password_resets"} password_reset GET /password_resets/:id(.:format) {:action=&gt;"show", :controller=&gt;"password_resets"} PUT /password_resets/:id(.:format) {:action=&gt;"update", :controller=&gt;"password_resets"} DELETE /password_resets/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"password_resets"} password_reset_path /password_resets/:id/edit(.:format) {:controller=&gt;"password_resets", :action=&gt;"edit"} /documents/:id/locations/create(.:format) {:controller=&gt;"locations", :action=&gt;"create"} permanent_delete_documents /documents/permanent_delete/:id(.:format) {:controller=&gt;"documents", :action=&gt;"permanently_delete"} document_authorinfo /documents/:document_id/authorinfo(.:format) {:controller=&gt;"documents", :action=&gt;"authorinfo"} document_publicationinfo /documents/:document_id/publicationinfo(.:format) {:controller=&gt;"documents", :action=&gt;"publicationinfo"} document_itemimages /documents/:document_id/images(.:format) {:controller=&gt;"documents", :action=&gt;"itemimages"} document_locations /documents/:document_id/locations(.:format) {:controller=&gt;"documents", :action=&gt;"locations"} document_itempeople /documents/:document_id/itempeople(.:format) {:controller=&gt;"documents", :action=&gt;"itempeople"} document_people_facts_loc /documents/:document_id/people_facts_locations(.:format) {:controller=&gt;"documents", :action=&gt;"people_facts_locations"} GET /documents/:document_id/locations(.:format) {:action=&gt;"index", :controller=&gt;"locations"} POST /documents/:document_id/locations(.:format) {:action=&gt;"create", :controller=&gt;"locations"} new_document_location GET /documents/:document_id/locations/new(.:format) {:action=&gt;"new", :controller=&gt;"locations"} edit_document_location GET /documents/:document_id/locations/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"locations"} document_location GET /documents/:document_id/locations/:id(.:format) {:action=&gt;"show", :controller=&gt;"locations"} PUT /documents/:document_id/locations/:id(.:format) {:action=&gt;"update", :controller=&gt;"locations"} DELETE /documents/:document_id/locations/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"locations"} document_people GET /documents/:document_id/people(.:format) {:action=&gt;"index", :controller=&gt;"document_people"} POST /documents/:document_id/people(.:format) {:action=&gt;"create", :controller=&gt;"document_people"} new_document_person GET /documents/:document_id/people/new(.:format) {:action=&gt;"new", :controller=&gt;"document_people"} edit_document_person GET /documents/:document_id/people/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"document_people"} document_person GET /documents/:document_id/people/:id(.:format) {:action=&gt;"show", :controller=&gt;"document_people"} PUT /documents/:document_id/people/:id(.:format) {:action=&gt;"update", :controller=&gt;"document_people"} DELETE /documents/:document_id/people/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"document_people"} document_document_facts GET /documents/:document_id/document_facts(.:format) {:action=&gt;"index", :controller=&gt;"document_facts"} POST /documents/:document_id/document_facts(.:format) {:action=&gt;"create", :controller=&gt;"document_facts"} new_document_document_fact GET /documents/:document_id/document_facts/new(.:format) {:action=&gt;"new", :controller=&gt;"document_facts"} edit_document_document_fact GET /documents/:document_id/document_facts/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"document_facts"} document_document_fact GET /documents/:document_id/document_facts/:id(.:format) {:action=&gt;"show", :controller=&gt;"document_facts"} PUT /documents/:document_id/document_facts/:id(.:format) {:action=&gt;"update", :controller=&gt;"document_facts"} DELETE /documents/:document_id/document_facts/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"document_facts"} document_facts POST /documents/:document_id/facts(.:format) {:action=&gt;"create", :controller=&gt;"facts"} new_document_facts GET /documents/:document_id/facts/new(.:format) {:action=&gt;"new", :controller=&gt;"facts"} edit_document_facts GET /documents/:document_id/facts/edit(.:format) {:action=&gt;"edit", :controller=&gt;"facts"} GET /documents/:document_id/facts(.:format) {:action=&gt;"show", :controller=&gt;"facts"} PUT /documents/:document_id/facts(.:format) {:action=&gt;"update", :controller=&gt;"facts"} DELETE /documents/:document_id/facts(.:format) {:action=&gt;"destroy", :controller=&gt;"facts"} document_document_photos GET /documents/:document_id/document_photos(.:format) {:action=&gt;"index", :controller=&gt;"document_photos"} POST /documents/:document_id/document_photos(.:format) {:action=&gt;"create", :controller=&gt;"document_photos"} new_document_document_photo GET /documents/:document_id/document_photos/new(.:format) {:action=&gt;"new", :controller=&gt;"document_photos"} edit_document_document_photo GET /documents/:document_id/document_photos/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"document_photos"} document_document_photo GET /documents/:document_id/document_photos/:id(.:format) {:action=&gt;"show", :controller=&gt;"document_photos"} PUT /documents/:document_id/document_photos/:id(.:format) {:action=&gt;"update", :controller=&gt;"document_photos"} DELETE /documents/:document_id/document_photos/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"document_photos"} documents GET /documents(.:format) {:action=&gt;"index", :controller=&gt;"documents"} POST /documents(.:format) {:action=&gt;"create", :controller=&gt;"documents"} new_document GET /documents/new(.:format) {:action=&gt;"new", :controller=&gt;"documents"} edit_document GET /documents/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"documents"} document GET /documents/:id(.:format) {:action=&gt;"show", :controller=&gt;"documents"} PUT /documents/:id(.:format) {:action=&gt;"update", :controller=&gt;"documents"} DELETE /documents/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"documents"} paypal_cancel /payments/cancel(.:format) {:controller=&gt;"payments", :action=&gt;"paypal_cancel"} paypal_return /payments/success(.:format) {:controller=&gt;"payments", :action=&gt;"paypal_return"} paypal_ipn /payments/ipn(.:format) {:controller=&gt;"payments", :action=&gt;"create"} document_search_home_index GET /home/document_search(.:format) {:action=&gt;"document_search", :controller=&gt;"home"} simple_location_search_home_index GET /home/simple_location_search(.:format) {:action=&gt;"simple_location_search", :controller=&gt;"home"} date_search_home_index GET /home/date_search(.:format) {:action=&gt;"date_search", :controller=&gt;"home"} simple_people_search_home_index GET /home/simple_people_search(.:format) {:action=&gt;"simple_people_search", :controller=&gt;"home"} simple_organisation_search_home_index GET /home/simple_organisation_search(.:format) {:action=&gt;"simple_organisation_search", :controller=&gt;"home"} document_filter_home_index GET /home/document_filter(.:format) {:action=&gt;"document_filter", :controller=&gt;"home"} search_results_home_index GET /home/search_results(.:format) {:action=&gt;"search_results", :controller=&gt;"home"} POST /home/search_results(.:format) {:action=&gt;"search_results", :controller=&gt;"home"} home_index GET /home(.:format) {:action=&gt;"index", :controller=&gt;"home"} POST /home(.:format) {:action=&gt;"create", :controller=&gt;"home"} new_home GET /home/new(.:format) {:action=&gt;"new", :controller=&gt;"home"} edit_home GET /home/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"home"} home GET /home/:id(.:format) {:action=&gt;"show", :controller=&gt;"home"} PUT /home/:id(.:format) {:action=&gt;"update", :controller=&gt;"home"} DELETE /home/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"home"} publicationinfo /documents/publicationinfo/:id(.:format) {:controller=&gt;"documents", :action=&gt;"publicationinfo"} publishinginfo /documents/update/publishinginfo/:id(.:format) {:controller=&gt;"documents", :action=&gt;"publishinginfo"} remove_image /documents/document_image_remove/:id(.:format) {:controller=&gt;"documents", :action=&gt;"remove_image"} make_primary_image /documents/make_primary_image/:id(.:format) {:controller=&gt;"documents", :action=&gt;"make_primary_image"} person_detail /person_detail/:id(.:format) {:controller=&gt;"documents", :action=&gt;"person_detail"} about /about(.:format) {:action=&gt;"about", :controller=&gt;"pages"} contact /contact(.:format) {:action=&gt;"contact", :controller=&gt;"pages"} privacy /privacy(.:format) {:action=&gt;"privacy", :controller=&gt;"pages"} terms /terms(.:format) {:action=&gt;"terms", :controller=&gt;"pages"} help /help(.:format) {:action=&gt;"help", :controller=&gt;"pages"} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload