使用设计创建下拉菜单注册页面

时间:2022-06-16 09:26:45

I would like to create a dropdown menu for a list of countries in my signup page with devise. I understand that I need to create a migration

我想使用设计为我的注册页面中的国家/地区列表创建一个下拉菜单。我知道我需要创建一个迁移

rails g migration add_countries_to_user country:string

and then I have to use create the form in my view page

然后我必须在我的视图页面中使用创建表单

<%= f.select :countries, options_for_select(%w[Alfganistan, Albania, Algeria...]) %>

I would like to know if my form correct and where can I put the countries list in because it is not right to write 200+ countries in the view page right?

我想知道我的表格是否正确以及我在哪里可以列出国家/地区列表,因为在视图页面中写200多个国家/地区是不对的?

Thanks.

5 个解决方案

#1


As suggested, you can use country_select. Or, you can do it on your own as:

如建议的那样,您可以使用country_select。或者,你可以自己做:

Create an initializer which contains list of countries (or anything in particular you want) config/initializers/countries.yml

创建一个初始化程序,其中包含国家/地区(或任何您特别需要的)config / initializers / countries.yml的列表

countries:
  - Afghanistan
  - United States
  - ...

Load it in database by creating a rake task as: lib/tasks/load_countries.rb

通过创建rake任务将其加载到数据库中:lib / tasks / load_countries.rb

namespace :db do
  desc "Loads countries in database"
  task :load_countries => :environment do |t|
    countries_list = YAML.load("#{Rails.root}/config/initializers/countries.yml")['countries']
    countries.each do |country|
      Country.find_or_create_by_name(country)
    end
  end
end

Whenever you add any countries in yml, you can populate it by invoking this rake task: rake db:load_countries.

无论何时在yml中添加任何国家/地区,都可以通过调用此rake任务来填充它:rake db:load_countries。

Maintain a model Country :

维护模型国家:

class Country < ActiveRecord::Base
  validates :name, presence: true, uniqueness: { case_insensitive: true }
end

I am considering that a user belongs_to 1 country above, and a country has_many users. In your view, :

我正在考虑用户属于上述1个国家/地区,并且国家/地区拥有多个用户。在您看来,:

f.select :country, options_from_collection_for_select(Country.all, :id, :name)

Note: I am using association approach above, since it will make it easier to make queries against this field in future, unlike saving an actual string in user.

注意:我正在使用上面的关联方法,因为它将在以后更容易对此字段进行查询,这与在用户中保存实际字符串不同。

#2


Use the country_select gem.

使用country_select gem。

# Gemfile
gem 'country_select'

form:

country_select("user", "country")

#3


Apart from gem and Countries read from YML.

除了宝石和国家从YML读取。

One more option is creating a method in your helper

还有一个选择是在助手中创建一个方法

File : app/helpers/country_helper.rb

文件:app / helpers / country_helper.rb

def get_countries
  {:1=>Africa,:2=>"America"}
end

In Views you can use this way

在视图中,您可以使用这种方式

<%= options_from_collection_for_select(get_countries, :id, :name) %>

#4


Look up rails cast #88 revised dynamic select menus. What you need is method call grouped_collection_select in which you will map out the item you need based on how they corresponded to one another

查看rails#88修订的动态选择菜单。你需要的是方法调用grouped_collection_select,你将在其中根据它们彼此对应的方式绘制出你需要的项目

#5


You could do this as a helper method. eg, in your users_helper.rb you could list the selections:

您可以将其作为辅助方法。例如,在您的users_helper.rb中,您可以列出选择:

def country_options
    [
        ['Afghanistan'],
        ['Albania'],
            ...
        ['Zimbabwe']
    ]
end

Then, your selector pulls from that helper method:

然后,您的选择器从该辅助方法中拉出:

<%= f.select :country, options_for_select(country_options), { prompt: 'Choose Country' } %>

#1


As suggested, you can use country_select. Or, you can do it on your own as:

如建议的那样,您可以使用country_select。或者,你可以自己做:

Create an initializer which contains list of countries (or anything in particular you want) config/initializers/countries.yml

创建一个初始化程序,其中包含国家/地区(或任何您特别需要的)config / initializers / countries.yml的列表

countries:
  - Afghanistan
  - United States
  - ...

Load it in database by creating a rake task as: lib/tasks/load_countries.rb

通过创建rake任务将其加载到数据库中:lib / tasks / load_countries.rb

namespace :db do
  desc "Loads countries in database"
  task :load_countries => :environment do |t|
    countries_list = YAML.load("#{Rails.root}/config/initializers/countries.yml")['countries']
    countries.each do |country|
      Country.find_or_create_by_name(country)
    end
  end
end

Whenever you add any countries in yml, you can populate it by invoking this rake task: rake db:load_countries.

无论何时在yml中添加任何国家/地区,都可以通过调用此rake任务来填充它:rake db:load_countries。

Maintain a model Country :

维护模型国家:

class Country < ActiveRecord::Base
  validates :name, presence: true, uniqueness: { case_insensitive: true }
end

I am considering that a user belongs_to 1 country above, and a country has_many users. In your view, :

我正在考虑用户属于上述1个国家/地区,并且国家/地区拥有多个用户。在您看来,:

f.select :country, options_from_collection_for_select(Country.all, :id, :name)

Note: I am using association approach above, since it will make it easier to make queries against this field in future, unlike saving an actual string in user.

注意:我正在使用上面的关联方法,因为它将在以后更容易对此字段进行查询,这与在用户中保存实际字符串不同。

#2


Use the country_select gem.

使用country_select gem。

# Gemfile
gem 'country_select'

form:

country_select("user", "country")

#3


Apart from gem and Countries read from YML.

除了宝石和国家从YML读取。

One more option is creating a method in your helper

还有一个选择是在助手中创建一个方法

File : app/helpers/country_helper.rb

文件:app / helpers / country_helper.rb

def get_countries
  {:1=>Africa,:2=>"America"}
end

In Views you can use this way

在视图中,您可以使用这种方式

<%= options_from_collection_for_select(get_countries, :id, :name) %>

#4


Look up rails cast #88 revised dynamic select menus. What you need is method call grouped_collection_select in which you will map out the item you need based on how they corresponded to one another

查看rails#88修订的动态选择菜单。你需要的是方法调用grouped_collection_select,你将在其中根据它们彼此对应的方式绘制出你需要的项目

#5


You could do this as a helper method. eg, in your users_helper.rb you could list the selections:

您可以将其作为辅助方法。例如,在您的users_helper.rb中,您可以列出选择:

def country_options
    [
        ['Afghanistan'],
        ['Albania'],
            ...
        ['Zimbabwe']
    ]
end

Then, your selector pulls from that helper method:

然后,您的选择器从该辅助方法中拉出:

<%= f.select :country, options_for_select(country_options), { prompt: 'Choose Country' } %>