如何在 rails 上的 ruby 中将位置值存储为 cookie

How to store location value as cookie in ruby on rails

我需要将所选位置值存储为 cookie 值。

我有一个位置 属性 table ,我从位置 table 获取并显示所有位置,所以当用户点击这个位置时,它应该搜索与那个位置。

现在正在搜索,但问题是如何将所选位置值存储为 cookie 值。

我的布局页面视图。

<%= form_tag location_search_path, :method=>'get' do %> 
<%= select_tag :location_id, options_from_collection_for_select(Location.all, :id, :name, params[:q]), :class=>"btn btn-primary dropdown-toggle" ,:onchange=>'this.form.submit()'%><br>
<% end %>

我的搜索控制器。

def location_search
    location=params[:location_id]
    @location = Location.find(location).name if params[:location_id].present?
    @property = Property.where(['location LIKE ?  AND status=?', "%#{@location}%", '3']).all
end

请help.Any帮忙欣赏一下table。

您可以使用 cookies[:cookie_name] = value 进行设置。因此,在您的情况下,您可能需要在控制器的 location_search 方法中使用类似的东西:

def location_search
  location=params[:location_id]
  @location = Location.find(location).name if params[:location_id].present?
  @property = Property.where(['location LIKE ?  AND status=?', "%#{@location}%", '3']).all

  cookies[:location] = @location
end

您可以在 rails 此处阅读有关 Cookie 的更多信息:http://guides.rubyonrails.org/action_controller_overview.html#cookies