Rails - NoMethoderror: Undefined method
Rails - NoMethoderror: Undefined method
我正在制作一个项目,允许用户按照 BinaryThistle 的教程 Here
select 他们正在参与的活动
输入“/users/1/edit”后,我收到以下错误:
NoMethodError in UsersController#edit
undefined method `userevents' for #<User:0x0000000c31f450>
app/controllers/users_controller.rb:14:in `edit'
我可以正确查看“/users/1”,没有任何错误。我也试过重新启动我的服务器和 运行 rake db:migrate.
users_controller.rb:
class UsersController < ApplicationController
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
helper_method :current_user
def show
end
def edit
@all_events = Event.all
@user_events = current_user.userevents.build
end
def update
redirect_to current_user
end
end
user.rb
class User < ActiveRecord::Base
class << self
def from_omniauth(auth_hash)
user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider'])
user.name = auth_hash['info']['name']
user.location = auth_hash['info']['location']
user.image_url = auth_hash['info']['image']
user.url = auth_hash['info']['urls']['user.provide.capitalize']
user.email = auth_hash['info']['email']
user.school = nil
has_many :userevents
has_many :events, :through => :userevents
user.save!
user
end
end
end
userevent.rb
class Userevent < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
event.rb
class Event < ActiveRecord::Base
has_many :userevents
has_many :users, :through => :userevents
end
请帮忙!
您必须在 User
class 中定义 has_many :userevents
。
将两个 has_many 关系放在方法之外。
把它写在这一行下面"class User < ActiveRecord::Base"。
希望对你有用
我正在制作一个项目,允许用户按照 BinaryThistle 的教程 Here
select 他们正在参与的活动输入“/users/1/edit”后,我收到以下错误:
NoMethodError in UsersController#edit
undefined method `userevents' for #<User:0x0000000c31f450>
app/controllers/users_controller.rb:14:in `edit'
我可以正确查看“/users/1”,没有任何错误。我也试过重新启动我的服务器和 运行 rake db:migrate.
users_controller.rb:
class UsersController < ApplicationController
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
helper_method :current_user
def show
end
def edit
@all_events = Event.all
@user_events = current_user.userevents.build
end
def update
redirect_to current_user
end
end
user.rb
class User < ActiveRecord::Base
class << self
def from_omniauth(auth_hash)
user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider'])
user.name = auth_hash['info']['name']
user.location = auth_hash['info']['location']
user.image_url = auth_hash['info']['image']
user.url = auth_hash['info']['urls']['user.provide.capitalize']
user.email = auth_hash['info']['email']
user.school = nil
has_many :userevents
has_many :events, :through => :userevents
user.save!
user
end
end
end
userevent.rb
class Userevent < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
event.rb
class Event < ActiveRecord::Base
has_many :userevents
has_many :users, :through => :userevents
end
请帮忙!
您必须在 User
class 中定义 has_many :userevents
。
将两个 has_many 关系放在方法之外。 把它写在这一行下面"class User < ActiveRecord::Base"。 希望对你有用