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
...