ruby-on-rails - ruby 或 rails 中有序数到基数的函数吗?

标签 ruby-on-rails ruby cucumber cardinality ordinals

我正试图找到一种更好的方式来表达我的 cucumber ,所以我正在寻找一个将其转换为基数的函数:

When I fill up the first passenger field
Then I should see the passenger list update with the first passenger details
When I follow "Add Another Passenger"
Then I should see a second passenger field
When I fill up the second passenger field
Then I should see the passenger list update with the second passenger details

进入更动态的东西(而不是为每一行创建单独的步骤)

这是我的网络步骤示例

When /^I fill up the first passenger field$/ do
  fill_in("booking_passengers_attributes_0_first_name", :with => "Blah")
  fill_in("booking_passengers_attributes_0_last_name", :with => "blah")
  select("5' to 6'", :from => "booking_passengers_attributes_0_height")
  select("100 to 150lbs", :from => "booking_passengers_attributes_0_weight")
end

When /^I fill up the second passenger field$/ do
  fill_in("booking_passengers_attributes_1_first_name", :with => "Wee")
  fill_in("booking_passengers_attributes_1_last_name", :with => "Sir")
  select("5' to 6'", :from => "booking_passengers_attributes_1_height")
  select("150 to 200lbs", :from => "booking_passengers_attributes_1_weight")
end

看到 0 和 1 了吗?我希望将“第一”转换为基数,这样我就可以替换了。您也可以建议一个更好的方法来声明 cukes :)

更新的答案 我正在进行重构,但基本上我使用了 1st 而不是 first 并在其上使用了 to_i。

When /^I fill up the "([^"]*)" passenger field$/ do |id|
  input_id = id.to_i - 1
  fill_in("booking_passengers_attributes_#{input_id}_first_name", :with => id)
  fill_in("booking_passengers_attributes_#{input_id}_last_name", :with => "Passenger")
  select("5' to 6'", :from => "booking_passengers_attributes_#{input_id}_height")
  select("100 to 150lbs", :from => "booking_passengers_attributes_#{input_id}_weight")  
end

最佳答案

我真的不完全明白你到底想做什么,但你可以在积极支持下做这样的事情:

 1.ordinalize    # => "1st"
  2.ordinalize    # => "2nd"
  1002.ordinalize # => "1002nd"

还有一个 Action View 助手 number_in_words 来获取“first”、“second”等

对不起,我不太了解杯子,

关于ruby-on-rails - ruby 或 rails 中有序数到基数的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5907084/

相关文章:

ruby-on-rails - 避免 Rspec + Capybara 中的代码重复

html - Rails Image_Tag 无法访问其他目录中的图像

c - 是否可以暂停/恢复部分全双工连接?

java - 在 : "Launching JCuc". java.lang.NullPointerException 期间发生内部错误

jquery - Sprockets::FileNotFound - 找不到文件 'jquery.ui'

ruby-on-rails - Rails 一对多 : Validate field value

ruby - 如何使用 Ruby 以编程方式获取我的 MAC 地址

java - @Before 不会在 java Cucumber Step 中执行

ruby - 为什么 Cucumber 被认为是集成测试工具而不是单元测试工具?

ruby-on-rails - ruby on rails 如何处理 NaN