在提交表单以在 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'
我的控制器中有问题的创建方法如下:
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
任何帮助是极大的赞赏。