【问题标题】:Rails country select gemRails 国家精选宝石
【发布时间】:2014-12-12 13:13:52
【问题描述】:

我正在使用国家/地区选择 gem,我想搜索基于他们国家/地区的用户,我已经尝试过此代码,但似乎 country_select_tag 不起作用,所以我想知道完成这项工作的最佳方法是什么

<%= form_tag search_users_path, class:"form-wrapper_compdirect cf navbar-left", :method => :get do %>

   <%= country_select_tag :country, params[:country] %> 
   <%= text_field_tag :query, params[:query], class:"quick-search_compdirect", placeholder:"Chercher des Utilisateurs" %> 
   <button type="submit" class="submit-button-search"><img src="/assets/search-g.png" title="Search" /></button>
<% end %>

并且还想知道如何获取国家/地区的名称而不是显示页面中的缩写,例如我的显示页面中有此代码

Pays de l'utilisateur : &lt;%= user.country %&gt; 但我没有得到法国,而是得到了 FR 做这项工作

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 search ruby-on-rails-4


    【解决方案1】:

    只需使用&lt;%= country_select :country, params[:country] %&gt; 而不是&lt;%= country_select_tag :country, params[:country] %&gt;

    【讨论】:

      【解决方案2】:

      你可以这样写

      country_select("user"#{model name}, "country"#{variable value})
      
      country_select("user", params[:country])
      

      您可以使用 country_select 文档中的某些方法获取国家/地区名称

      检查此链接一次:http://www.rubydoc.info/gems/country_select/2.1.0/frames

      【讨论】:

      • 在我的应用程序助手中,我有这个代码 def country_name country = ISO3166::Country[country_code] country.translations[I18n.locale.to_s] || country.name end 和我的显示页面 但我收到此错误 undefined method `country_name'
      【解决方案3】:

      我尝试了第一个答案,但由于表单已共享并出现在结果页面上,因此任何后续表单提交都会破坏查询字符串。

      <%= form_tag(search_path, method: :get) do %>
        <label for="country">Search by Country</label>
        <%= country_select nil, :country, { selected: params[:country] } %>
        <%= submit_tag("Submit") %>
      <% end %>
      

      所以现在,如果表单被共享,将发送回正确的参数,并使用所选国家/地区更新字段。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-04
        • 1970-01-01
        相关资源
        最近更新 更多