简体   繁体   English

Ruby on Rails会话变量在第一次渲染时未显示在前端

[英]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]'. 我正在使用add_to_box方法将一组值添加到称为“ session [:box]”的会话变量中。 I then want to update the partial on the front end with the new list of values. 然后,我想用新的值列表更新前端的partial。

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: add_to_box.js.erb:

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

Partial _box.html.erb: _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: 第一次调用该方法(值1,1,1)时,前端显示为:

∙ , , ∙,,

The second time the method is called (values 2,2,2), the front end displays as: 第二次调用该方法(值2、2、2)时,前端显示为:

∙ 1, 1, 1 ∙1,1,1

∙ , , ∙,,

The third time the method is called (values 3,3,3), the front end displays as: 第三次调用该方法(值3、3、3)时,前端显示为:

∙ 1, 1, 1 ∙1,1,1

∙ 2, 2, 2 ∙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 ∙1,1,1

∙ 2, 2, 2 ∙2,2,2

∙ 3, 3, 3 ∙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? 作为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: 我在session [:box]上添加了logger.debug,并得到了以下内容:

{"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. 奇怪的是,我没有抓住它的原因是,刷新页面时它正在调用相同的session [:box]变量并显示正常。 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 如果有人可以对此分享一些见识会很感兴趣

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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