2011-12-25 60 views
0

所有我是新的轨道。 我想销毁在分组表中有group_id的用户。为什么它发生错误,找不到用户发生,当我从组表

我的源代码在这里。

GroupsController

def leave 
    @user = current_user 
    @group = Group.find(params[:id]) 
    @user.groupings.find_by_group_id(@group).destroy 
    redirect_to :back , notice: "Destroy!" 
end 

lists.html.haml

%td= link_to("Destroy",leave_group_path(:id => group),:confirm => "Are you  sure",:method => :delete) 

当我点击 “消灭” 按钮,然后就发生错误Couldn't find User with id=1 我要摧毁只有分组表,但为什么找不到current_user.I不删除current_user。

错误日志是

app/controllers/application_controller.rb:6:in `current_user' 

而且CURRENT_USER提供OmniAuth插件的是helper_method。

CURRENT_USER实现以下这个..

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    private 

    def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
    end 

    helper_method :current_user 
end 

请一些帮助。

在此先感谢。

+0

它是否肯定会触发该控制器操作?回溯显示错误发生在哪条线上?组播控制器上是否有过滤器? – 2011-12-25 18:09:33

+0

什么是current_user方法和/或您使用的是什么自动系统? – andrewpthorp 2011-12-25 19:30:17

+0

谢谢,我已经更新了我的问题内容以添加“错误日志”和current_user方法。请看这个。 – nobinobiru 2011-12-26 05:09:16

回答

0

如果有错误说could not find user with id=1那么没有id = 1的用户。 Ofte我们删除/销毁用户。

在轨当你破坏与ID = 1记录并创建另一个记录,然后将新创建的记录ID不会是1。你可能会遇到这种情况。

此外,您在模型协会是这样的,我猜测::

用户模型

class User < ActiveRecord::Base

has_many :groups#不分组

end

组模型#提到我问题。

class Group < ActiveRecord::Base

belongs_to :user

end

这意味着::

@user.groupings.find_by_group_id(@group).destroy也不可能是正确的。

而是尝试这种::

@user.groups.find_by_group_id(@group).destroy

,因为用户的has_many组和不分组。

Read More about naming conventions

+0

谢谢你问我的问题。但分组是中间表。所以user.rb'has_many:groups,:through =>:groupings',group.rb'has_many:through =>:groupings',并且参加动作,删除动作的反例如下... '@user.groupings。创建!(:group_id =>组) – nobinobiru 2011-12-26 01:30:41

相关问题