为什么我在尝试列出 rails 数组的键的值时得到 Cookie 溢出和 nil 参数?

Why do I get a Cookie Overflow and a nil argument when trying to list the values for a key of a rails array?

我在 messages.html.erb 中有以下代码。

<% vars = request.query_parameters
   @templates = session[:templates]
%>

<h3>There are <%= @templates.count %> templates</h3>
<form method='post' action='http://localhost:80/text.php'>

 <select name="template">
   <% @templates.each do |t| %>
      <option name='<%=t.name %>'><%= t.name %></option>
   <% end %>
 </select>
  <br /><br />
  <input type='submit' value='Submit' />
</form>

现在,在application_helper.rb中,有一个方法link_to_phone,其中我们有:

  session[:templates] = Template.all
  link_to(h(phone), message_path(phone: phone, id: contact.id, page: 'contacts'), title: phone)

其中 message_path 将我们带到 messages.html.erb。

如果部分(我省略了 phone、id 和 page 字段,因为它们在这里不起作用):

 <select name="template">
   <% @templates.each do |t| %>
      <option name='<%=t.name %>'><%= t.name %></option>
   <% end %>
messages.html.erb 中省略了

,我得到的声明是,在我的例子中,link_to_phone 定义在会话中存储了 3 个模板。如果该部分包含在代码中,那么我会得到一个 Cookie 溢出。据我所知,我没有使用 cookie,而是认为我正在使用会话。我在 view/template/index.html.erb 中有另一个与此非常相似的语句,其中我循环遍历 @templates 并显示名称、消息和主题,除了放置单独的模板名称外,使用完全相同的想法, msg,然后主题进入 table 没问题。

我在这里遗漏了什么导致这个 'Cookie' 问题?

会话实际上是 cookie,但信息存储在服务器端,cookie 与会话 ID 一起传递到用户的浏览器。整个 cookie 的大小限制为 4KB,包括名称、值、有效期。

但是Rails默认存储session信息在cookie_store,你可以通过改变你的session_store来解决这个问题,例如你可以使用active_record_store。

参见:Cookie overflow in rails application?