Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 控制器中的命名错误#新_Ruby On Rails_Controller_Nomethoderror - Fatal编程技术网

Ruby on rails 控制器中的命名错误#新

Ruby on rails 控制器中的命名错误#新,ruby-on-rails,controller,nomethoderror,Ruby On Rails,Controller,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_s

大家好,我在我的应用程序中工作,我遇到了这个错误

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' 
如你所见,方法是有“加油站”

我的控制器上只有这个

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

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
class用户
正如错误所说,Rails不知道加油站是什么,因为它属于用户。您需要设置该关联:

class User ...
  ...   
  has_many :gas_stations
  ...

我仍然没有得到这个错误:nil:nilclass的未定义方法'build'应该
加油站从哪里来?您是否将其定义为有许多关系?否则,就没有这样的方法类gastationclass User ...
  ...   
  has_many :gas_stations
  ...