相关疑难解决方法(0)

访问模型中的current_user

我有3张桌子

items (columns are:  name , type)
history(columns are: date, username, item_id)
user(username, password)
Run Code Online (Sandbox Code Playgroud)

当用户说"ABC"登录并创建新项目时,将使用以下after_create过滤器创建历史记录.如何通过此过滤器将此用户名"ABC"分配给历史记录表中的用户名字段.

class Item < ActiveRecord::Base
  has_many :histories
  after_create :update_history
  def update_history
    histories.create(:date=>Time.now, username=> ?) 
  end
end
Run Code Online (Sandbox Code Playgroud)

我在session_controller中的登录方法

def login
  if request.post?
    user=User.authenticate(params[:username])
    if user
      session[:user_id] =user.id
      redirect_to( :action=>'home')
      flash[:message] = "Successfully logged in "
    else
      flash[:notice] = "Incorrect user/password combination"
      redirect_to(:action=>"login")
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我没有使用任何身份验证插件.如果有人能告诉我如何在不使用插件(如userstamp等)的情况下实现这一点,我将不胜感激.

ruby-on-rails

39
推荐指数
4
解决办法
4万
查看次数

标签 统计

ruby-on-rails ×1