简体   繁体   中英

Ruby on Rails Session Variable not Showing on Front End on First Render

I'm having a strange issue where the last value in a session array is not displaying on the front end when a partial is rendered for the first time. I'm using the add_to_box method to add a set of values to a session variable called 'session[:box]'. I then want to update the partial on the front end with the new list of values.

Here's the code... (sanitised slightly)

Controller:

def add_to_box(values_hash)

  session[:box] ||= []
  if params_valid?(values_hash)

    session[:box].push(values_hash)

    respond_to do |format|
      format.js
    end

  end

end

add_to_box.js.erb:

$("#box").html("<%= escape_javascript(render partial: 'box') %>");

Partial _box.html.erb:

<ul>
  <% session[:box].each do |m| %>
    <li>
      <%= "#{m['bottom_id']}, #{m['filling_id']}, #{m['top_id']}" %>
    </li>
  <% end %>
</ul>

Console output:

Started POST "/things/add_to_box" for ::1 at 2016-08-09 11:59:23 +0100
Processing by ThingController#add_to_box as JS
Rendered things/_box.html.erb (0.9ms)
Rendered things/add_to_box.js.erb (2.8ms)
Completed 200 OK in 17ms (Views: 8.2ms | ActiveRecord: 0.7ms)

The first time the method is called (values 1,1,1), the front end displays as:

∙ , ,

The second time the method is called (values 2,2,2), the front end displays as:

∙ 1, 1, 1

∙ , ,

The third time the method is called (values 3,3,3), the front end displays as:

∙ 1, 1, 1

∙ 2, 2, 2

∙ , ,

You get the picture... Essentially the latest value is never rendered.

BUT if I refresh the page, it shows correctly with all values:

∙ 1, 1, 1

∙ 2, 2, 2

∙ 3, 3, 3

I thought at first it could be because the values aren't saved in time and the render is happening first, but then how does it know there is a new set of values before they have been set?!

Need some help on this one

I have figured it out. As part of the params_valid? method I was doing the following:

  values_hash[:top_id] = params[:top_id].to_i
  values_hash[:filling_id] = params[:filling_id].to_i
  values_hash[:bottom_id] = params[:bottom_id].to_i

I added a logger.debug on session[:box] and got the following:

{"top_id"=>1, "filling_id"=>1, "bottom_id"=>1, "id"=>1}
{"top_id"=>1, "filling_id"=>1, "bottom_id"=>1, "id"=>2}
{"top_id"=>1, "filling_id"=>1, "bottom_id"=>1, "id"=>3}
{"top_id"=>1, "filling_id"=>1, "bottom_id"=>1, "id"=>4}
{:top_id=>1, :filling_id=>1, :bottom_id=>1, :id=>5}

You'll notice the difference in the last row. I changed the code above to be the following and it worked:

  values_hash["top_id"] = params[:top_id].to_i
  values_hash["filling_id"] = params[:filling_id].to_i
  values_hash["bottom_id"] = params[:bottom_id].to_i

The strange thing about this, and the reason I didn't catch it was that when refreshing the page it was calling the same session[:box] variable and displaying fine. Something must be happening to the session variable in the background to tidy it up.

Would be interested if anyone could share some insight on this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM