我是Ruby和Rails的新手,我很难获得选择下拉菜单,先显示当前年份,然后按降序返回30年。我使用form_for生成表单/字段。
我可以通过以下方式按升序运行:
<%= builder.label :modelyear, "Model Year" %>
<%= builder.select :modelyear, (Time.zone.now.year - 30)..(Time.zone.now.year + 0) %>
这些不起作用:
<%= builder.label :modelyear, "Model Year" %>
<%= builder.select :modelyear, (Time.zone.now.year)..(Time.zone.now.year - 30) %>
<%= builder.label :modelyear, "Model Year" %>
<%= builder.select :modelyear, (Time.zone.now.year + 0)..(Time.zone.now.year - 30) %>
编辑:根据要求填写完整代码(原始尝试选择器)
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<br /><br />
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<br /><br />
<%= f.label :email %>
<%= f.text_field :email %>
<br /><br />
<%= f.label :address1 %>
<%= f.text_field :address1 %>
<br /><br />
<%= f.label :address2 %>
<%= f.text_field :address2 %>
<br /><br />
<%= f.label :city %>
<%= f.text_field :city %>
<br /><br />
<%= f.label :state %>
<%= f.text_field :state %>
<br /><br />
<%= f.label :zipcode %>
<%= f.text_field :zipcode %>
<br /><br />
<%= f.label :country %>
<%= f.text_field :country %>
<br /><br />
<!-- Fields For Product -->
<%= f.fields_for :products do |builder| %>
<div>
<%= builder.label :serial, "Serial Number" %>
<%= builder.text_field :serial %>
<br /><br />
<%= builder.label :modelyear, "Model Year" %>
<%= builder.select :modelyear, (Time.zone.now.year - 30)..(Time.zone.now.year + 0) %>
<br /><br />
</div>
<% end %>
<%= f.submit "Register Your Product", class: "button" %>
user.rb
class User < ActiveRecord::Base
has_many :products, dependent: :destroy
accepts_nested_attributes_for :products, :allow_destroy => true
before_save { email.downcase! }
validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
validates :address1, presence: true
validates :address2, length: { maximum: 30 }
validates :city, presence: true
validates :state, length: { maximum: 20 }
validates :zipcode, length: { maximum: 20 }
validates :country, presence: true
end
product.rb
class Product < ActiveRecord::Base
belongs_to :user
validates :serial, presence: true
validates :modelyear, length: { maximum: 4 }
end
答案 0 :(得分:0)
试试这个:
<%= builder.select :modelyear, Time.zone.now.year.downto(Time.zone.now.year - 30)
.to_a, class:'form-control' %>
答案 1 :(得分:0)