I''m looking to store the current params into a/some cookie(s). This works: @params.each_pair do |this_index, this_param| cookies[this_index] = this_param end Except, I get session-only cookies. This doesn''t work: @params.each_pair do |this_index, this_param| cookies[this_index] = { :value => this_param, :expires => 30.days.from_now } end Specifcally, I get the error:> private method `gsub'' called for ["Sonic", "0"]:ArrayI''m not entirely sure I understand why one of these works, but the other doesn''t. Can someone point me in the right direction here? -------------- next part -------------- An HTML attachment was scrubbed... URL: wrath.rubyonrails.org/pipermail/rails/attachments/20060306/a226f153/attachment.html
I''ve been poking around with this. I''m starting to think this may be a minor bug in the cookie module... Here''s what I''ve got now: I''m converting an older app from PHP to Rails. The users makes a few choices and I display/filter the data to match their choices. I want to store their choices in a cookie, so they remain "stuck" the next time the visitor returns to the site. The problem: Storing a Hash in the cookie with an expiration gives a "NoMethodError" -- not finding "gsub" on the Hash/Array (?). More specifically: The user makes a choice as to which book to search in, the source list comes from the database, and uses abbreviations to track which source is selected. I converted this almost straight from PHP: <% @optional_sources.each do |this_source| -%> <input type="checkbox" name="add_sources[]" value="<%=this_source.abbrev%>" id="<%=this_source.abbrev%>"<%''checked="checked"'' if @cookies[''add_source''].index(this_source.abbrev) %> /> <label for="<%=this_source.abbrev%>"><%=this_source.name%></label><br /> <% end -%> This returns a @params array like this: @params[''add_sources''] = [ ''ABBREV1'', ''ABBREV2'', ''ABBREV3'' ] Of course, this isn''t a very "Railsian" approach. So I implemented the next set of choices (let''s say, choosing a city) using the check_box helper: <p><b>Cities:</b> <ul><% cities = %w{ Toronto Chicago } cities.each do |this_city| %> <li> <%= check_box ("cities", this_city) %> <label for="elements_<%=this_city%>"><%=this_city%></label> </li> <% end -%> </ul> Which produces a @params hash like this: @params[''cities''][''Toronto''] = 0 @params[''cities''][''Chicago''] = 1 Or, to put it another way, like this: @params[''cities''] = { ''Toronto'' => 0, ''Chicago'' => 1 } The problem (at long last): In the first case (sources), this works fine: cookies[:sources] = { :value => @params[:add_sources], :expires => 30.days.from_now } In the second case (cities), this DOES work: cookies[:cities] = @params[:cities] BUT, creates a this-session-only cookie. Trying to set an expiration: cookies[:sources] = { :value => @params[:cities], :expires => 30.days.from_now } ... produces the gsub error.
in the rails book it says cookie values can only have string types otherwise you''ll run into the gsub error. Agile Rails page 312 you might want to stringify your hash and store it in the cookie. Then when extracting it, parse it back into your hash object. On 3/7/06, Josh on Rails <rails@thewehners.net> wrote:> > I''ve been poking around with this. I''m starting to think this may be a > minor bug in the cookie module... Here''s what I''ve got now: > > I''m converting an older app from PHP to Rails. The users makes a few > choices and I display/filter the data to match their choices. I want > to store their choices in a cookie, so they remain "stuck" the next > time the visitor returns to the site. > > The problem: Storing a Hash in the cookie with an expiration gives a > "NoMethodError" -- not finding "gsub" on the Hash/Array (?). > > More specifically: > The user makes a choice as to which book to search in, the source list > comes from the database, and uses abbreviations to track which source > is selected. I converted this almost straight from PHP: > > <% @optional_sources.each do |this_source| -%> > <input type="checkbox" name="add_sources[]" > value="<%=this_source.abbrev%>" id="<%=this_source.abbrev%>"<%> ''checked="checked"'' if > @cookies[''add_source''].index(this_source.abbrev) %> /> <label > for="<%=this_source.abbrev%>"><%=this_source.name%></label><br /> > <% end -%> > > This returns a @params array like this: > > @params[''add_sources''] = [ ''ABBREV1'', ''ABBREV2'', ''ABBREV3'' ] > > Of course, this isn''t a very "Railsian" approach. So I implemented the > next set of choices (let''s say, choosing a city) using the check_box > helper: > > <p><b>Cities:</b> > <ul><% > cities = %w{ Toronto Chicago } > cities.each do |this_city| > %> > <li> <%= check_box ("cities", this_city) %> <label > for="elements_<%=this_city%>"><%=this_city%></label> </li> > <% end -%> > </ul> > > Which produces a @params hash like this: > > @params[''cities''][''Toronto''] = 0 > @params[''cities''][''Chicago''] = 1 > > Or, to put it another way, like this: > > @params[''cities''] = { ''Toronto'' => 0, ''Chicago'' => 1 } > > > The problem (at long last): > In the first case (sources), this works fine: > > cookies[:sources] = { :value => @params[:add_sources], :expires => > 30.days.from_now } > > In the second case (cities), this DOES work: > > cookies[:cities] = @params[:cities] > > BUT, creates a this-session-only cookie. Trying to set an expiration: > > cookies[:sources] = { :value => @params[:cities], :expires => > 30.days.from_now } > > ... produces the gsub error. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: wrath.rubyonrails.org/pipermail/rails/attachments/20060405/a65347d0/attachment-0001.html
Seemingly Similar Threads
- [PATCH] Fix various -Wformat problems.
- [PATCH v2] Fix various -Wformat problems.
- [PATCH nbdkit v2 0/3] Rewrite xz plugin as a filter.
- [PATCH nbdkit 0/2] Rewrite xz plugin as a filter.
- [PATCH 0/3] Timezone and keyboard layout settings in virt-builder and virt-sysprep.