0

So this one has me stumped; hopefully a kind soul can help me out. As part of logic to display certain buttons for users who have performed an action, I store the id of the object they manipulated in a session array called "prayed_for". (The unique part of this problem is that it deals with sessions and not an array persisted in a database.) In the show action of my controller, I evaluate whether of not the current id of the entry being requested is present in the session array "prayed_for". I assign this boolean value to the session variable @already_prayed_for. Below is the logic for that:

@already_prayed_for = (session[:prayed_for] ||= []).include? params[:id]

But here's the problem: I cannot evaluate this in my partial. For example if I attempt to evaluate the following (in HAML), where "entry" is a variable representing the entry at hand and "id" is it's id, which should be stored in the session "prayed_for" variable, it will always evaluate to false:

-if (session[:prayed_for] ||= []).include? entry.id

I've come to the conclusion that I may be evaluating something wrong in my partial when evaluating whether or not an id is present in a session array. Additionally this same concept worked perfectly in a controller action (but I can't use that solution this time around, it has to be evaluated in the partial) but it also failed in the ApplicationHelper. Any help in resolving this problem is much appreciated!

UPDATE: Here's the code where I set the session in another action:

if @entry.save
  (session[:prayed_for] ||= []) << params[:id]
end
2
  • Can you also post the line(s) of code where you set the session? Commented Jan 22, 2012 at 6:04
  • Please see my update on the question for those lines of code :) Commented Jan 22, 2012 at 6:06

1 Answer 1

1

params[:id] may be a String, while entry.id is a Fixnum. Verify that the objects you're comparing (via include?) are of the same type.

You might want to make a helper out of the logic you're using, for example:

def already_prayed_for?(entry_id)
  (session[:prayed_for] ||= []).include? entry_id.to_i
end
Sign up to request clarification or add additional context in comments.

2 Comments

I understand the problem now! Thank you! One quick question: I'm using Mongoid an don't have access to .to_i. Is there any other method I could use that does the same thing?
I found that I had to use .to_s in the helper you provided to make everything work with the parameters string representations. Thank you so much for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.