What I want to implement this time is a function that stores unchanged information such as the shipping source and product status as Active_Hash and displays it as a pull-down in the view file when implementing the product listing function.
Regarding how to store prefectures etc. with Active_Hash, I wrote a separate article, so please refer to that.
It is one of the helper methods, and it is a description that can display the value given the key in the select box.
The basic description is
ruby:xxx.html.erb
f.collection_select(:Column name,Array of elements,Item of value attribute,Text item, { prompt: "Please select" }
It seems to be.
This time, I will write down an example of implementing the presence or absence of shipping charges as a PostageType class when listing products.
ruby:products/new.html.erb
<%= f.collection_select(:postage_type, PostageType.all, :id, :name, {}, {class:"select-box", id:"item-shipping-fee-status"}) %>
If you leave the prompt part empty, Active Hash's: id = 1 seems to be displayed as the pull-down default.
I will also write down the description of Active Hash of postage_type.
models/postage_type.rb
class PostageType < ActiveHash::Base
self.data = [
{id: 1, name: '---'},{id: 2, name: 'Cash on delivery(Buyer burden)'},{id: 3, name: 'postage included(Exhibitor burden)'}
]
end
As an aside, let's write the ** class name in the upper camel case **. I made an error and got lost once ...
Recommended Posts