0
我目前正在使用mongoid和rails进行项目。下面是代码:@不允许作为实例变量名称
class Account
include Mongoid::Document
include Mongoid::Timestamps
field :account_name, type: String
has_many :groups
end
class Group
include Mongoid::Document
field :group_name, type: String
belongs_to :account
has_and_belongs_to_many :groups
end
class GroupsController < ApplicationController
before_filter :require_login, :find_company
def new
@group = @company.groups.new
end
def create
@group = @company.groups.new params[:group]
if @group.save
redirect_to people_path
else
render :new
end
end
private
def find_company
@company = current_account.groups.find(params[:company_id]) if params[:company_id]
end
end
和正在返回的错误是:
@' is not allowed as an instance variable name (NameError)
./app/controllers/groups_controller.rb:5:in `new'
我似乎无法找到太多google搜索周围的问题,但它会出现问题在于has_and_belongs_to_many关系,但我不确定。
任何想法,将不胜感激。
感谢
我想如果'params [:company_id]'返回nil,那么你会在'nil'对象上调用'groups.new',错误也会发生。 – Zippie 2013-04-18 07:39:54
您使用的是什么Ruby版本? 'ruby -v' – 2013-04-18 07:40:11
有一次你可以试试这个吗?在'new'行动中'@group = @ company.groups.build'。 – 2013-04-18 10:22:27