Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8805281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:49:37+00:00 2026-06-14T01:49:37+00:00

I have a small web app in Ruby on Rails which lists antique documents

  • 0

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

undefined method `locations' for nil:NilClass

The NilClass occurs in LocationController#new:

def new
  @location = @document.locations.new
end

This is called from the view as such

<%= form_for(@location,:url=>new_document_location_path(@document)) do |f| %>
  <%= render :partial => "document_locations/form", :locals =>{:f=>f} %>
  <%= f.submit "Save",:class=>"span2 btn" %>
<%end%>

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 <%= @document.id %>

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.

This is how the models are defined

class Document < ActiveRecord::Base
  has_many :locations, :dependent => :destroy
end

class Location < ActiveRecord::Base
  belongs_to :document
end

Here’s my routes

Pastpaper::Application.routes.draw do

  # added in to test
  match '/documents/:id/locations/create' => 'locations#create'
  resources :documents do 
    collection do 
      match 'permanent_delete/:id' => 'documents#permanently_delete',:as => :permanent_delete
    end

    match '/authorinfo' => 'documents#authorinfo',:as => :authorinfo

    match '/publicationinfo' => 'documents#publicationinfo',:as => :publicationinfo
    match '/images' => 'documents#itemimages' ,:as => :itemimages
    match '/locations' => 'documents#locations',:as => :locations
    match '/itempeople'  => 'documents#itempeople' ,:as => :itempeople
    match '/people_facts_locations' => 'documents#people_facts_locations',:as => :people_facts_loc
    resources :locations, :controller=>"locations"
    resources :people ,:controller => "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' => 'documents#publicationinfo',:as => :publicationinfo
  match 'documents/update/publishinginfo/:id' => 'documents#publishinginfo',:as => :publishinginfo 
  match 'documents/document_image_remove/:id' => 'documents#remove_image',:as=>"remove_image"
  match 'documents/make_primary_image/:id' => 'documents#make_primary_image',:as => :make_primary_image
  match 'person_detail/:id' => 'documents#person_detail',:as=>'person_detail'

  match 'about', :to=> 'pages#about'
  match 'contact', :to => 'pages#contact'
  match 'privacy', :to => 'pages#privacy'
  match 'terms', :to => 'pages#terms'
  match 'help', :to => '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' => 'Documents#restore_document', :as => 'restore_document'
    match 'report' => 'report#index' ,:as=>:report
    match 'report/surname_report' => 'report#surname_report',:as=>:surname_report
    match 'report/location_report' => 'report#location_report',:as=>:location_report
  end
  root :to => 'home#index'

end

And here’s the relevant Controllers:
DocumentController

class DocumentsController < ApplicationController
  before_filter :prepare_document ,:only => [: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 => [:document_attributes])
    elsif params[:document_id]
      @document = Document.find(params[:document_id], :include => [:document_attributes])
    end  
  end
end

LocationsController

class LocationsController < 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 => [:document_attributes])
      else
        @document = current_user.documents.find(params[:document_id], :include => [:document_attributes])
      end  
    end  
  end
end

This is how my routes rake out

                           logout GET    /logout(.:format)                                          {:action=>"destroy", :controller=>"sessions"}
                            login POST   /login(.:format)                                           {:action=>"create", :controller=>"sessions"}
                  forgot_password        /forgot_password(.:format)                                 {:action=>"new", :controller=>"password_resets"}
                        user_home GET    /profile(.:format)                                         {:action=>"index", :controller=>"users"}
                deactivateaccount        /account/deactivate(.:format)                              {:controller=>"users", :action=>"accountdeactivate"}
                   changepassword        /account/changepassword(.:format)                          {:controller=>"users", :action=>"changepassword"}
                         register        /user/registration(.:format)                               {:controller=>"users", :action=>"new"}
                            users GET    /users(.:format)                                           {:action=>"index", :controller=>"users"}
                                  POST   /users(.:format)                                           {:action=>"create", :controller=>"users"}
                         new_user GET    /users/new(.:format)                                       {:action=>"new", :controller=>"users"}
                        edit_user GET    /users/:id/edit(.:format)                                  {:action=>"edit", :controller=>"users"}
                             user GET    /users/:id(.:format)                                       {:action=>"show", :controller=>"users"}
                                  PUT    /users/:id(.:format)                                       {:action=>"update", :controller=>"users"}
                                  DELETE /users/:id(.:format)                                       {:action=>"destroy", :controller=>"users"}
                  password_resets GET    /password_resets(.:format)                                 {:action=>"index", :controller=>"password_resets"}
                                  POST   /password_resets(.:format)                                 {:action=>"create", :controller=>"password_resets"}
               new_password_reset GET    /password_resets/new(.:format)                             {:action=>"new", :controller=>"password_resets"}
              edit_password_reset GET    /password_resets/:id/edit(.:format)                        {:action=>"edit", :controller=>"password_resets"}
                   password_reset GET    /password_resets/:id(.:format)                             {:action=>"show", :controller=>"password_resets"}
                                  PUT    /password_resets/:id(.:format)                             {:action=>"update", :controller=>"password_resets"}
                                  DELETE /password_resets/:id(.:format)                             {:action=>"destroy", :controller=>"password_resets"}
              password_reset_path        /password_resets/:id/edit(.:format)                        {:controller=>"password_resets", :action=>"edit"}
                                         /documents/:id/locations/create(.:format)                  {:controller=>"locations", :action=>"create"}
       permanent_delete_documents        /documents/permanent_delete/:id(.:format)                  {:controller=>"documents", :action=>"permanently_delete"}
              document_authorinfo        /documents/:document_id/authorinfo(.:format)               {:controller=>"documents", :action=>"authorinfo"}
         document_publicationinfo        /documents/:document_id/publicationinfo(.:format)          {:controller=>"documents", :action=>"publicationinfo"}
              document_itemimages        /documents/:document_id/images(.:format)                   {:controller=>"documents", :action=>"itemimages"}
               document_locations        /documents/:document_id/locations(.:format)                {:controller=>"documents", :action=>"locations"}
              document_itempeople        /documents/:document_id/itempeople(.:format)               {:controller=>"documents", :action=>"itempeople"}
        document_people_facts_loc        /documents/:document_id/people_facts_locations(.:format)   {:controller=>"documents", :action=>"people_facts_locations"}
                                  GET    /documents/:document_id/locations(.:format)                {:action=>"index", :controller=>"locations"}
                                  POST   /documents/:document_id/locations(.:format)                {:action=>"create", :controller=>"locations"}
            new_document_location GET    /documents/:document_id/locations/new(.:format)            {:action=>"new", :controller=>"locations"}
           edit_document_location GET    /documents/:document_id/locations/:id/edit(.:format)       {:action=>"edit", :controller=>"locations"}
                document_location GET    /documents/:document_id/locations/:id(.:format)            {:action=>"show", :controller=>"locations"}
                                  PUT    /documents/:document_id/locations/:id(.:format)            {:action=>"update", :controller=>"locations"}
                                  DELETE /documents/:document_id/locations/:id(.:format)            {:action=>"destroy", :controller=>"locations"}
                  document_people GET    /documents/:document_id/people(.:format)                   {:action=>"index", :controller=>"document_people"}
                                  POST   /documents/:document_id/people(.:format)                   {:action=>"create", :controller=>"document_people"}
              new_document_person GET    /documents/:document_id/people/new(.:format)               {:action=>"new", :controller=>"document_people"}
             edit_document_person GET    /documents/:document_id/people/:id/edit(.:format)          {:action=>"edit", :controller=>"document_people"}
                  document_person GET    /documents/:document_id/people/:id(.:format)               {:action=>"show", :controller=>"document_people"}
                                  PUT    /documents/:document_id/people/:id(.:format)               {:action=>"update", :controller=>"document_people"}
                                  DELETE /documents/:document_id/people/:id(.:format)               {:action=>"destroy", :controller=>"document_people"}
          document_document_facts GET    /documents/:document_id/document_facts(.:format)           {:action=>"index", :controller=>"document_facts"}
                                  POST   /documents/:document_id/document_facts(.:format)           {:action=>"create", :controller=>"document_facts"}
       new_document_document_fact GET    /documents/:document_id/document_facts/new(.:format)       {:action=>"new", :controller=>"document_facts"}
      edit_document_document_fact GET    /documents/:document_id/document_facts/:id/edit(.:format)  {:action=>"edit", :controller=>"document_facts"}
           document_document_fact GET    /documents/:document_id/document_facts/:id(.:format)       {:action=>"show", :controller=>"document_facts"}
                                  PUT    /documents/:document_id/document_facts/:id(.:format)       {:action=>"update", :controller=>"document_facts"}
                                  DELETE /documents/:document_id/document_facts/:id(.:format)       {:action=>"destroy", :controller=>"document_facts"}
                   document_facts POST   /documents/:document_id/facts(.:format)                    {:action=>"create", :controller=>"facts"}
               new_document_facts GET    /documents/:document_id/facts/new(.:format)                {:action=>"new", :controller=>"facts"}
              edit_document_facts GET    /documents/:document_id/facts/edit(.:format)               {:action=>"edit", :controller=>"facts"}
                                  GET    /documents/:document_id/facts(.:format)                    {:action=>"show", :controller=>"facts"}
                                  PUT    /documents/:document_id/facts(.:format)                    {:action=>"update", :controller=>"facts"}
                                  DELETE /documents/:document_id/facts(.:format)                    {:action=>"destroy", :controller=>"facts"}
         document_document_photos GET    /documents/:document_id/document_photos(.:format)          {:action=>"index", :controller=>"document_photos"}
                                  POST   /documents/:document_id/document_photos(.:format)          {:action=>"create", :controller=>"document_photos"}
      new_document_document_photo GET    /documents/:document_id/document_photos/new(.:format)      {:action=>"new", :controller=>"document_photos"}
     edit_document_document_photo GET    /documents/:document_id/document_photos/:id/edit(.:format) {:action=>"edit", :controller=>"document_photos"}
          document_document_photo GET    /documents/:document_id/document_photos/:id(.:format)      {:action=>"show", :controller=>"document_photos"}
                                  PUT    /documents/:document_id/document_photos/:id(.:format)      {:action=>"update", :controller=>"document_photos"}
                                  DELETE /documents/:document_id/document_photos/:id(.:format)      {:action=>"destroy", :controller=>"document_photos"}
                        documents GET    /documents(.:format)                                       {:action=>"index", :controller=>"documents"}
                                  POST   /documents(.:format)                                       {:action=>"create", :controller=>"documents"}
                     new_document GET    /documents/new(.:format)                                   {:action=>"new", :controller=>"documents"}
                    edit_document GET    /documents/:id/edit(.:format)                              {:action=>"edit", :controller=>"documents"}
                         document GET    /documents/:id(.:format)                                   {:action=>"show", :controller=>"documents"}
                                  PUT    /documents/:id(.:format)                                   {:action=>"update", :controller=>"documents"}
                                  DELETE /documents/:id(.:format)                                   {:action=>"destroy", :controller=>"documents"}
                    paypal_cancel        /payments/cancel(.:format)                                 {:controller=>"payments", :action=>"paypal_cancel"}
                    paypal_return        /payments/success(.:format)                                {:controller=>"payments", :action=>"paypal_return"}
                       paypal_ipn        /payments/ipn(.:format)                                    {:controller=>"payments", :action=>"create"}
       document_search_home_index GET    /home/document_search(.:format)                            {:action=>"document_search", :controller=>"home"}
simple_location_search_home_index GET    /home/simple_location_search(.:format)                     {:action=>"simple_location_search", :controller=>"home"}
           date_search_home_index GET    /home/date_search(.:format)                                {:action=>"date_search", :controller=>"home"}
  simple_people_search_home_index GET    /home/simple_people_search(.:format)                       {:action=>"simple_people_search", :controller=>"home"}
simple_organisation_search_home_index GET    /home/simple_organisation_search(.:format)                 {:action=>"simple_organisation_search", :controller=>"home"}
       document_filter_home_index GET    /home/document_filter(.:format)                            {:action=>"document_filter", :controller=>"home"}
        search_results_home_index GET    /home/search_results(.:format)                             {:action=>"search_results", :controller=>"home"}
                                  POST   /home/search_results(.:format)                             {:action=>"search_results", :controller=>"home"}
                       home_index GET    /home(.:format)                                            {:action=>"index", :controller=>"home"}
                                  POST   /home(.:format)                                            {:action=>"create", :controller=>"home"}
                         new_home GET    /home/new(.:format)                                        {:action=>"new", :controller=>"home"}
                        edit_home GET    /home/:id/edit(.:format)                                   {:action=>"edit", :controller=>"home"}
                             home GET    /home/:id(.:format)                                        {:action=>"show", :controller=>"home"}
                                  PUT    /home/:id(.:format)                                        {:action=>"update", :controller=>"home"}
                                  DELETE /home/:id(.:format)                                        {:action=>"destroy", :controller=>"home"}
                  publicationinfo        /documents/publicationinfo/:id(.:format)                   {:controller=>"documents", :action=>"publicationinfo"}
                   publishinginfo        /documents/update/publishinginfo/:id(.:format)             {:controller=>"documents", :action=>"publishinginfo"}
                     remove_image        /documents/document_image_remove/:id(.:format)             {:controller=>"documents", :action=>"remove_image"}
               make_primary_image        /documents/make_primary_image/:id(.:format)                {:controller=>"documents", :action=>"make_primary_image"}
                    person_detail        /person_detail/:id(.:format)                               {:controller=>"documents", :action=>"person_detail"}
                            about        /about(.:format)                                           {:action=>"about", :controller=>"pages"}
                          contact        /contact(.:format)                                         {:action=>"contact", :controller=>"pages"}
                          privacy        /privacy(.:format)                                         {:action=>"privacy", :controller=>"pages"}
                            terms        /terms(.:format)                                           {:action=>"terms", :controller=>"pages"}
                             help        /help(.:format)                                            {:action=>"help", :controller=>"pages"}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:49:39+00:00Added an answer on June 14, 2026 at 1:49 am
    def new
      @location = @document.locations.build
    end
    

    Use build not new. And make your prepare_document private in your controller.

    Also

    <%= form_for([@document, @location]) do |f| %>
    

    Your url parameter was wrong, you shouldn’t post forms to the new action, you should post them to the update action like I showed above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have small web app that generate PDF files as a report. I'm trying
In my web app I have a line which looks like this: small textbox
i've developed small web app for personal use with Ruby on Rails. Now when
I have a small web app that is nothing more than a few HTML
I have a developed a small web-app in Symfony 2 and Doctrine 2. Can
I'm building a small personal web app in Ruby on Rails. I've set it
Hi I'm developing a small web app in which I have and embed video
I have a small web app that uses a simple username/password login scheme. I've
I have a small web app I wrote that has a Dijit.layout.BorderContainer nested within
I have a small web app that I'm in the process of deploying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.