NoMethodError в контроллере#новый

Привет всем, я работаю в своем приложении, и я столкнулся с этой ошибкой

NoMethodError in GasStationsController#new

undefined method `gas_stations' for #<Class:0x12cf77510>
Rails.root: /Users/Users/Documents/myapps/app1

Application Trace | Framework Trace | Full Trace
app/controllers/gas_stations_controller.rb:4:in `new' 

Я не понимаю, если мои маршруты в порядке. Ставлю свои route.rb и свой GasStationsController

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 :gas_stations
    end
    ....

user_gas_stations GET    /users/:user_id/gas_stations(.:format)                       gas_stations#index
                      POST   /users/:user_id/gas_stations(.:format)                       gas_stations#create
 new_user_gas_station GET    /users/:user_id/gas_stations/new(.:format)                   gas_stations#new
edit_user_gas_station GET    /users/:user_id/gas_stations/:id/edit(.:format)              gas_stations#edit
     user_gas_station GET    /users/:user_id/gas_stations/:id(.:format)                   gas_stations#show
                      PUT    /users/:user_id/gas_stations/:id(.:format)                   gas_stations#update
                      DELETE /users/:user_id/gas_stations/:id(.:format)                   gas_stations#destroy

как видите метод есть "gas_stations"

у меня такое только на контроллере

class GasStationsController < ApplicationController
     def new
      @user = User.find(params[:user_id])
      @gas_station = @user.gas_stations.build
     end
end

Пользовательская модель

class User < ActiveRecord::Base
 # Include default devise modules. Others available are:
 # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
 devise :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable

 # Setup accessible (or protected) attributes for your model
 attr_accessible :name, :email, :password, :password_confirmation, :remember_me
 # attr_accessible :title, :body
 has_many :cars
 validates_presence_of  :email
end

person BlackSan    schedule 27.07.2012    source источник
comment
все еще ничего, я получаю эту ошибку: неопределенный метод `build' для nil: NilClass   -  person BlackSan    schedule 28.07.2012
comment
откуда должно взяться gas_stations? вы определили это как отношение has_many? в противном случае есть NoSuchMethod   -  person phoet    schedule 28.07.2012
comment
класс GasStation ‹ ActiveRecord::Base has_many :tanking_logs   -  person BlackSan    schedule 28.07.2012


Ответы (1)


Как говорится в ошибке, Rails понятия не имеет, что такое gas_stations применительно к пользователю. Вам нужно установить эту ассоциацию:

class User ...
  ...   
  has_many :gas_stations
  ...
person Carson Cole    schedule 27.07.2012