简体   繁体   中英

LIne too long error for hash - Rails Haml

= f.association :country, :collection => ProductValues.countries.map { |country| [country.name, country.id] }, :include_blank => "Select Country", :label => "Country", :selected => records.country_id ? records.country_id : crecord.country_id

When I commit my code I am getting line too long error. I can split the above code in multiple lines. How can I implement a best practise to avoid such an error in the above line.

The best practice is to avoid calculation logic in view, one pattern usually used in Rails project is decorator pattern. This gem can be used to refactor using that pattern: https://github.com/drapergem/draper

An easy way to improve above code is to separate calculations into separate line

- country_map = ProductValues.countries.map { |country| [country.name, country.id] }
- selected = records.country_id ? records.country_id : crecord.country_id

= f.association :country, collection: country_map, include_blank: "Select Country", label: "Country", selected: selected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM