Ruby on Rails类变量未被控制器引用

时间:2012-11-09 12:47:08

标签: ruby-on-rails

我在Ruby on Rails中有一个小型Web应用程序,它列出了古董文档和文档引用的位置。我可以更新已附加到文档的位置,但在尝试创建新文档时,我收到以下错误

undefined method `locations' for nil:NilClass

NilClass出现在LocationController#new:

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

这是从视图中调用的

<%= 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%>

因此,由于某种原因,控制器未引用@document类。我知道该类可以在视图和范围内访问,因为我能够在表单中查看文档属性(例如&lt;%= @ document.id%&gt;

那么,为什么Controller无法引用@document变量?我假设这是我传递类变量的方式。无论如何,我会非常感谢一些指示。

这就是模型的定义方式

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

class Location < ActiveRecord::Base
  belongs_to :document
end

这是我的路线

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

这是相关的控制器: 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

这就是我的路线耙出来的方式

                           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 个答案:

答案 0 :(得分:1)

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

使用build而不是new。并在控制器中将prepare_document设为私有。

另外

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

您的网址参数错误,您不应该将表单发布到新操作,您应该将其发布到我上面显示的更新操作。