Hi there guys! I''m having problems with sessions. Ive created a sessions table via the <strong>db:sessions:create</strong>. Now what happens is that whenever i maneuver around my site, it keeps on creating new sessions. I just want one session per user to store their user ids. I was just wondering where i was going wrong. thanks! Attachments: http://www.ruby-forum.com/attachment/960/session.gif -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
come on lads! you gotta help me! cry... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maybe you could provide some code to help us better understand what you are doing. My app, for example, creates a session variable when a user logs in or creates an account like so: session[:user] = User.authenticate(params[:current_user][:emailaddr], params[:current_user][:password]).id Then it gets destroyed when they leave. Do you have some kind of before_filter in your model that keeps hitting that part of the code that makes the session variable? This would result in a session variable continiously being created. For example, to ensure that a user is who they say they are when they try to navigate to a certain page in my app, I have apllied a filter that does a check before they can view that page: before_filter :check_authentication, :except => [. . . .] Do you have something similiar that makes use of a filter and then creates a session variable? Good luck, -S -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for replying Shandy! Im a bit of a noob! I dont have any filters yet! Do i need to? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
To use sessions and the session hash all you need to do is create a session variable like so: session[:key_name] = ... Everything else is automatic, at least if you haven''t touched the original Rails settings. You don''t need to use filters to use sessions, but for user authentication for example it is probably a good idea. Regards Erik Lindblad On 12 Nov, 23:53, Serkan Serkan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thanks for replying Shandy! > > Im a bit of a noob! I dont have any filters yet! Do i need to? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 end def mark_one @school = School.find(params[:id]) @school.update_attribute :rating_sum, @school.rating_sum += 1 @school.update_attribute :rating_num, @school.rating_num += 1 session[:voted] = true respond_to do |format| if @school.save flash[:notice] = ''School was successfully rated.'' format.html { redirect_to school_url(@school) } format.xml { head :created, :location => school_url(@school) } else format.html { redirect_to school_url(@school) } format.xml { render :xml => @school.errors.to_xml } end end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Try session[:voted] ||= false This will only set voted to false if voted is not set. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here''s where it gets tricky. People can clear sessions and then vote more than once for schools. The Large Majority (tm) don''t know how to do that, but we have to be careful for The Large Minority (tm) that can teach them how to! You can either: Store it as a session variable: session[:voted] ||= {} session[:voted][@school.id] = true Or store it as a table, which is so messy I would rather you do it in a session variable. On Dec 5, 2007 1:38 PM, Ellis Berner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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. > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Would this make going directly to /schools/1/mark_rating_as_one come up as an error because the session hash is not instantiated? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yes. You might want to put: session[:voted] ||= {} Somewhere were it will always be set. On Dec 5, 2007 2:44 PM, Ellis Berner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Would this make going directly to /schools/1/mark_rating_as_one come up > as an error because the session hash is not instantiated? > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
<% if session[:voted][:school_id] == false %> <%= link_to "1", mark_one_school_path(@school), :method => :put %> <%= link_to "2", mark_two_school_path(@school), :method => :put %> <%= link_to "3", mark_three_school_path(@school), :method => :put %> <%= link_to "4", mark_four_school_path(@school), :method => :put %> <%= link_to "5", mark_five_school_path(@school), :method => :put %> <% end %> is my view def show session[:voted] ||= {} session[:voted][@school.id] = false ... end def mark_as_one session[:voted][@school.id] = true ... end This does not work. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. school.rb: has_many :votes user.rb belongs_to :vote Then you should be able to go <% if current_user.vote.nil? %> instead of relying on the unreliable session. On Dec 5, 2007 2:59 PM, Ellis Berner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > <% if session[:voted][:school_id] == false %> > <%= link_to "1", mark_one_school_path(@school), :method => :put %> > <%= link_to "2", mark_two_school_path(@school), :method => :put %> > <%= link_to "3", mark_three_school_path(@school), :method => :put %> > <%= link_to "4", mark_four_school_path(@school), :method => :put %> > <%= link_to "5", mark_five_school_path(@school), :method => :put %> > <% end %> > > is my view > > def show > session[:voted] ||= {} > session[:voted][@school.id] = false > ... > end > > def mark_as_one > session[:voted][@school.id] = true > ... > end > > This does not work. > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It is actually a very big design issue that there be no users. What other solutions are we looking at? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Dec 5, 2007 3:07 PM, Ellis Berner <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > It is actually a very big design issue that there be no users. What > other solutions are we looking at? > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Is this as concise as I can get it? def mark_five Vote.create(:school_id => @school.id, :ip_address => request.remote_addr) ... end def show @school = School.find(params[:id]) @course = Course.new @courses = Course.find_all_by_school_id(params[:id]) @votes = Vote.find_all_by_school_id(params[:id]) @vote = false for vote in @votes if vote.ip_address == request.remote_addr @vote = true break else @vote = false end end end <% if @vote = false %> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Everything up to the for seems good. The for could be improved right down to one line. @vote = Vote.find_all_by_school_id(params[:id]).detect { |v| v.ip_address =request.remote_addr } Here, @vote will either be nil (indicating no vote), or the first (and hopefully only) vote object it comes across matching that IP address. Your if statement should check to see: <% if @vote.nil? %> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---