在提交表单以在rails应用程序中创建新对象时,我收到以下错误:
NoMethodError in MembersController#create
undefined method `group' for #<Member:0x007fadb5222e40>
Application Trace | Framework Trace | Full Trace
app/controllers/members_controller.rb:58:in `block in create'
app/controllers/members_controller.rb:57:in `create'
我的控制器中有问题的create方法如下:
def create
@group = Group.find(params[:group_id])
@user = User.find(session[:user_id])
if @user and @group and @group.group_password == params[:password]
@member = Member.new do |m|
m.group_id = @group.id
m.user_id = @user.id
m.slug = @user.username
end
logger.debug("#{ @member.group_id}, #{ @member.user_id}, #{ @member.slug}")
respond_to do |format|
if @member.save
format.html { redirect_to @group, notice: 'Member was successfully created.' }
format.json { render json: @member, status: :created, location: @member }
else
format.html { render action: "new" }
format.json { render json: @member.errors, status: :unprocessable_entity }
end
end
else
redirect_to group_join_url(@group), alert: "Invalid group password"
end
end
错误中提到的第57行是“respond_to do | format |”
只是为了好的衡量,这是我的会员模块:
class Member < ActiveRecord::Base
extend FriendlyId
friendly_id :slug, :use => :scoped, :scope => :group
attr_accessible :group_id, :slug, :user_id
belongs_to :groups
belongs_to :users
end
非常感谢任何帮助。
答案 0 :(得分:0)
我可能会以错误的方式查看内容,但似乎您有:group
的范围,而您的模型则属于:groups
(复数)。似乎没有任何关于您的用户类的组属性的指示 - 但是如果关联是单数的,则会有一个属性。