I currently have below lines for application_controller.rb. Basically, I am using session to check whether user has logged in or not, including authentication. I just used a term m
class ApplicationController < ActionController::Base
protect_from_forgery
protected
# Returns the currently logged in member or nil if there isn't one
def current_member
return unless session[:member_id]
@current_member ||= Member.find_by_id(session[:member_id])
end
# Make current_member available in templates as a helper
helper_method :current_member
# Filter method to enforce a login requirement
# Apply as a before_filter on any controller you want to protect
def authenticate
logged_in? ? true : access_denied
end
# Predicate method to test for a logged in member
def logged_in?
current_member.is_a? Member
end
# Make logged_in? available in templates as a helper
helper_method :logged_in?
def access_denied
redirect_to login_path, :notice => "Please log in to continue" and return false
end
end
And I have a class called article, where users can write some articles. In database table configuration, this article table has a column name called member_id which basically records the unique id value of each member as an author. I did belongs_to/has_many linking, but I am now stuck on article.rb model validation. How can I approach the member_id using the above code?? Can I just say it like
class Article < ActiveRecord::Base
member_id = current_member.id
end