I’m trying to make it so each user can only vote once. By making an if
statement in the view that looks at the boolean of session[:voted] and
then decides weather to display the content. However, the problem is the
boolean resets itself every time it reloads the show action. How do I
fix this?
def show @school = School.find(params[:id]) @courses = Course.find_all_by_school_id(params[:id])
session[:voted] = false
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @school }
end
Ok, that worked great thanks. However, there is more than one School in
my collection. Changing session[:voted] affects how it behaves for all
schools. How do I make it specific to each school?
Ryan wrote:
Try session[:voted] ||= false
This will only set voted to false if voted is not set.
People can clear sessions and then vote more than once for schools. The
Large Majority ™ don’t know how to do that, but we have to be careful
for
The Large Minority ™ that can teach them how to!
Do users log into your site? Perhaps it would be better to store it in a
votes table with just two fields, a school_id and a user_id. That way we
don’t get the nasty session hash errors when the hash isn’t defined.
instead of storing user_ids store IP addresses. This is easier to
maintain
as you really only have to define one relationship (the one on the
school).
You have two fields in the votes table still, school_id and ip_address.
To
get an IP address you can do request.remote_addr.