You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sinatra应用问题:如何让final页面显示所有选中的复选框而非仅最后一个

Fixing Sinatra Checkbox Issue: Show All Selected Items on Final Page

Hey there! Let's sort out that checkbox issue in your Sinatra app where only the last selected topping shows up on the final page. The problem comes down to how HTML forms and Sinatra handle multiple values with the same name—here's how to fix it step by step:

1. Update Your Form's Checkbox Names

First, you need to tell the browser that your checkboxes are part of an array. In your namesex.erb template, change the name attribute of each checkbox to end with []:

<!-- Example checkbox group in namesex.erb -->
<div>
  <h3>Select Toppings:</h3>
  <label>
    <input type="checkbox" name="toppings[]" value="pepperoni"> Pepperoni
  </label>
  <label>
    <input type="checkbox" name="toppings[]" value="mushrooms"> Mushrooms
  </label>
  <label>
    <input type="checkbox" name="toppings[]" value="extra_cheese"> Extra Cheese
  </label>
</div>

This tells the browser to send all selected checkbox values as an array instead of overwriting the value with the last selected item.

2. Adjust Your Sinatra Route to Handle the Array

Next, update your app.rb to properly capture the array of toppings and pass it to the final page. When you use name="toppings[]", Sinatra will automatically populate params[:toppings] as an array (or nil if no boxes are checked):

# app.rb
get '/' do
  erb :namesex
end

post '/namesex' do
  # Redirect to final page with all form parameters, including the toppings array
  redirect to('/final', params: {
    firstname: params[:firstname],
    lastname: params[:lastname],
    sirname: params[:sirname],
    toppings: params[:toppings]
  })
end

get '/final' do
  # Assign parameters to instance variables for the template
  @firstname = params[:firstname]
  @lastname = params[:lastname]
  @sirname = params[:sirname]
  # Handle cases where no toppings were selected to avoid nil errors
  @toppings = params[:toppings] || []
  erb :final
end

3. Render All Toppings in the Final Template

Finally, update your final.erb template to loop through the @toppings array and display every selected item:

<!-- final.erb -->
<h1>Your Submission</h1>
<p><strong>First Name:</strong> <%= @firstname %></p>
<p><strong>Last Name:</strong> <%= @lastname %></p>
<p><strong>Sir Name:</strong> <%= @sirname %></p>

<p><strong>Selected Toppings:</strong></p>
<ul>
  <% @toppings.each do |topping| %>
    <li><%= topping.humanize %></li> <!-- .humanize makes "extra_cheese" into "Extra Cheese" -->
  <% end %>
</ul>

Why This Works

  • The [] in the checkbox name signals to the browser that multiple values should be sent as an array.
  • Sinatra recognizes this array format and stores all selected values in params[:toppings] instead of just the last one.
  • We handle the edge case where no toppings are selected by setting @toppings to an empty array if params[:toppings] is nil, preventing template errors.

内容的提问来源于stack exchange,提问作者zachary edgell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:41:29