I'm new to both Ruby and Rails and am having trouble getting a select drop down to show the current year first and then go back 30 years in descending order. I'm using form_for to generate the form/fields.
我是Ruby和Rails的新手,我很难通过选择下拉来显示当前年份,然后按降序返回30年。我正在使用form_for来生成表单/字段。
I can get it to work in ascending order with the following:
我可以通过以下方式按升序工作:
<%= builder.label :modelyear, "Model Year" %>
<%= builder.select :modelyear, (Time.zone.now.year - 30)..(Time.zone.now.year + 0) %>
These do not work:
这些不起作用:
<%= 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) %>
Edit: complete code as requested (with original attempt at selector)
编辑:按要求完成代码(原始尝试选择器)
<%= 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
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
product.rb
class Product < ActiveRecord::Base
belongs_to :user
validates :serial, presence: true
validates :modelyear, length: { maximum: 4 }
end
2 个解决方案
#1
0
This should work
这应该工作
<%= builder.select :modelyear, ((Time.zone.now.year - 30)..(Time.zone.now.year + 0)).to_a.reverse %>
Here I am converting the Range
object to an Array
, which is also accepted by select as parameter. So you can use the convenient array reverse method.
这里我将Range对象转换为Array,select也作为参数接受。所以你可以使用方便的数组反向方法。
#2
0
Try this:
尝试这个:
<%= builder.select :modelyear, Time.zone.now.year.downto(Time.zone.now.year - 30)
.to_a, class:'form-control' %>
#1
0
This should work
这应该工作
<%= builder.select :modelyear, ((Time.zone.now.year - 30)..(Time.zone.now.year + 0)).to_a.reverse %>
Here I am converting the Range
object to an Array
, which is also accepted by select as parameter. So you can use the convenient array reverse method.
这里我将Range对象转换为Array,select也作为参数接受。所以你可以使用方便的数组反向方法。
#2
0
Try this:
尝试这个:
<%= builder.select :modelyear, Time.zone.now.year.downto(Time.zone.now.year - 30)
.to_a, class:'form-control' %>