目前加入了很多rails 3的练习,我正在开发一个认证系统,并且正在关注railscasts上的教程。来自railscast的Ryan对该教程做了一些更新,并稍作修改以利用rails 3.1代码何时属于模型中的红宝石轨?
has_secure_password
因此,一些在我的Sessions_controller的代码更改为:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_username(params[:username])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_path, :notice => "Logged In"
else
flash.now.alert = "Invalid Credentials"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_path, :notice =>"Logged Out"
end
end
我想知道的是,如果一些代码的创建方法/动作应该是在模型?在这里使用这些代码是好的还是不好的做法?
我应该遵循什么规则,因为我想要学习正确的方法,而不是捡到坏习惯,因为我已经超越了学习框架的一部分,在这个框架中,事情开始比平常更有意义。
建议是欣赏..
我想特别知道的是..
1.当没有一个程序员的代码时,在模型中属于知道些什么?他/她如何做出这个决定?
谢谢,这个答案为我解决了一些问题。 – LondonGuy