开发者

RoR - password protected model

I want to 开发者_如何学运维create password protected model. For example Post on the blog. I want to store this password in the database. And if user wants to see password protected post he needs to write this password. If there is no password in database everyone can see this post, each post can have its own pass. How can I create something like this in RoR? I

I only have found basic HTTP auth:

before_filter :authenticate

#protected

def authenticate
  authenticate_or_request_with_http_basic do |username, password|
    username == "foo" && password == "bar"
  end
end

but probably there is better solution for this? Do you have any ideas?


Something like this ?

def show
  @post = Post.find(...)

  if params[:post][:password].nil?
    # Show a form with a password asked
  elsif params[:post][:password] == @post.password
    # Show post
  else
    flash[:error] = "Bad password"
    # Render password form
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜