Rails 5-具有动态国家/地区输入的标志图标 [英] Rails 5 - Flag Icons with dynamic country input

查看:41
本文介绍了Rails 5-具有动态国家/地区输入的标志图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的Rails 5应用程序中安装了这个标志图标scss gem.

I have installed this flag icon scss gem in my Rails 5 app.

https://github.com/eugenegarl/flag-icons-rails

我现在正在尝试找出如何在我的Address模型中使用它,以便帮助者从Address表中获取国家/地区名称,并使用该国家/地区名称显示正确的标志.我找不到一种将动态输入提供给标志助手的方法.

I'm trying now to figure out how I can use it with my Address model so that the helper gets the country name from the Address table and uses that to display the correct flag. I can't find a way to give dynamic input to the flag helper.

宝石的自述文件中有一条我无法理解的说明:

The gem's readme has an instruction which I can't make sense of:

Do not add gem to assets section if you want to use flag_icon rails helper.

什么是资产?我将宝石添加到宝石文件中,捆绑并添加:

What are the assets? I added the gem to the gemfile, bundled and then added:

@import "flag-icon";

到我的application.scss-这是资产.我已经尝试从application.scss中删除该导入行,以防自述文件中的指令表示如果您要使用帮助程序,则不要使用该行.删除该行并不能解决我的问题(因此又回来了).

to my application.scss - which is an asset. I have tried removing that import line from application.scss in case the instruction in the readme means not to use that line if you want to use the helper. Removing that line didn't resolve my problem (so it's back in).

我有一个地址模型,里面有一个国家(地区)属性.

I have an address model, which has a country attribute in it.

a = Address.last.country
  Address Load (0.9ms)  SELECT  "addresses".* FROM "addresses" ORDER BY "addresses"."id" DESC LIMIT $1  [["LIMIT", 1]]
 => "AU" 

我希望能够在标志图标帮助器中使用国家",以便呈现相应的标志.

I want to be able to use "country" in the flag icon helper so that it renders the corresponding flag.

在我看来,我已经尝试过:

In my view, I have tried:

        <%= flag_icon(Address.last.country) %>

        <%= flag_icon(Address.last.country.to_sym) %>

这些尝试均无效.它们不会产生任何错误,只是不会渲染地图.

Neither of these attempts work. They don't produce any errors, they just don't render a map.

地图有效,因为如果我尝试:

The maps work, because if i try:

<%= flag_icon(:au) %>

它会打印正确的标志.

如何为助手提供动态的国家/地区输入?

How can I give the helper a dynamic country input?

推荐答案

遇到类似问题.

我使用 gem'country_select'.

结果证明,该符号位于CAPS中,因此无法正常工作.

Turns out, the symbol was in CAPS, so wasn't working.

尝试更改以下内容:

<%= flag_icon(Address.last.country.to_sym)%>

收件人:

<%= flag_icon(Address.last.country.downcase.to_sym)%>

(请注意上面添加的 .downcase ).

(Notice the .downcase added above).

这篇关于Rails 5-具有动态国家/地区输入的标志图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆