Ruby: 3.3.3
Hanami: 2.1.1
Following is a method I have in my view scope
def update_form
form_for('update-form', '/update_data', method: :patch) do |f|
tag.div(class: 'form-control') do
tag.label('Update Form')
end
tag.div(class: 'form-control') do
tag.label("Select Age")
tag.select(name: :age, class: 'age') do
tag.option( 'Select an Age', value: empty_string )
[ 20, 25, 30, 35 ].each do |age|
tag.option( age, value: age )
end
end
end
tag.div(class: "form-control") do
f.button("Update", type: 'button', class: "button-primary update-btn")
f.button('Cancel', type: 'button', class: "button-primary cancel-update-btn")
end
end
end
In view-template app/templates/..../update_data.html.haml
I have following code:
%div.update-form-container<
= update_form
When I access the page in browser and inspect the DOM it contains following markup:
<div class="update-form-container">
<form action="/update_data" method="POST" accept-charset="utf-8">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_csrf_token" value="195da3c1adfeeaf482800a3f29b1ddcc65185f08a98e3a7971ef59bdc2916ced">
<div class="form-control">
<button type="button" class="button-primary cancel-update-btn">Cancel</button>
</div>
</form>
</div>
Experimenting with the code in form_for
block found that only the last evaluated markup is considered as the content.
For e.g. if I modify update_form
method as following
def update_form
form_for('update-form', '/update_data', method: :patch) do |f|
tag.div(class: 'form-control') do
tag.label('Update Form')
end
tag.div(class: 'form-control') do
tag.label("Select Age")
tag.select(name: :age, class: 'age') do
tag.option( 'Select an Age', value: empty_string )
[ 20, 25, 30, 35 ].each do |age|
tag.option( age, value: age )
end
end
end
end
end
and then refreshing the page when I inspected the DOM it contains following markup:
<div class="update-form-container">
<form action="/update_data" method="POST" accept-charset="utf-8">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_csrf_token" value="195da3c1adfeeaf482800a3f29b1ddcc65185f08a98e3a7971ef59bdc2916ced">
<div class="form-control">
<select name="age" class="age">[20, 25, 30, 35]</select>
</div>
</form>
</div>
Can anybody please guide me on the correct usage of form_for
so as to generate following desired markup
<div class="update-form-container">
<form action="/update_data" method="POST" accept-charset="utf-8">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_csrf_token" value="195da3c1adfeeaf482800a3f29b1ddcc65185f08a98e3a7971ef59bdc2916ced">
<div class="form-control">
<label>Update Form</label>
</div>
<div class="form-control">
<label>Select Age</label>
<select name='age', class: 'age'>
<option value=''>Select an Age</option>
<option value='20'>20</option>
<option value='25'>25</option>
<option value='30'>30</option>
<option value='35'>35</option>
</select>
</div>
<div class="form-control">
<button type="button" class="button-primary update-btn">Update</button>
<button type="button" class="button-primary cancel-update-btn">Cancel</button>
</div>
</form>
</div>