Ruby on Rails:从输入字段保存会话变量

时间:2023-02-06 08:20:02

How do I save a session variable (maybe into a cookie) when a user inputs a location? I'd like to keep this information for future sign ins. So they won't have to retype their location multiple times.

当用户输入位置时,如何保存会话变量(可能是cookie)?我想保留这些信息以备将来登录。因此,他们不必多次重新键入其位置。

<%= form_tag search_path, :method => :get do %>
  <%= text_field_tag :q, params[:q], :class => "search" %>
  <%= text_field_tag :loc, params[:loc], :class => "search-main", :id => "loc-search", :placeholder => "address, neighborhood, city, state or zip" %>
  <%= button_tag :type => :submit, :class => "btn btn-medium btn-inverse" do %>
    <i class="icon-search icon-white"></i>
  <% end %>
<% end %>

This is in my layouts/application.html.erb

这是在我的layouts / application.html.erb中

I'd have to set a variable if it hasn't been set yet, and display it in the field if it is already set

如果尚未设置变量,我必须设置变量,如果已经设置,则将其显示在字段中

2 个解决方案

#1


2  

You can use cookies:

你可以使用cookies:

cookies[:name] = {
  value: 'my_cookie',
  expires: 1.year.from_now,
}

See more here.

在这里查看更多。

You can also use session_store like this.

您也可以像这样使用session_store。

#2


1  

have you tried:

你有没有尝试过:

session[:location] = params[:loc]

#1


2  

You can use cookies:

你可以使用cookies:

cookies[:name] = {
  value: 'my_cookie',
  expires: 1.year.from_now,
}

See more here.

在这里查看更多。

You can also use session_store like this.

您也可以像这样使用session_store。

#2


1  

have you tried:

你有没有尝试过:

session[:location] = params[:loc]