如何显示所有选中的复选框,而不是仅显示最后选中的复选框

时间:2018-04-14 02:26:02

标签: html css ruby sinatra erb

namesex page

final page

我希望它显示所有选中的复选框,而不是最后选中的复选框 原谅final.erb fistnamelastname的工作原理sirname我只是想看看能否让我的所有复选框都能正常工作

app.rb

require "sinatra"

get '/' do 
    erb :namesex
end

post '/namesex' do 
    firstname = params[:firstname]
    lastname = params[:lastname]
    sirname = params[:sirname]
    pizzacrust = params[:pizzacrust]
    toppings = params[:toppings]
    redirect '/final?firstname=' + firstname + '&lastname=' + lastname + '&sirname=' + sirname + '&pizzacrust=' + pizzacrust + '&toppings=' + toppings
end

get '/final' do 
    firstname = params[:firstname]
    lastname = params[:lastname]
    sirname = params[:sirname]
    pizzacrust = params[:pizzacrust]
    toppings = params[:toppings]
    erb :final, :locals => {:firstname => firstname, :lastname => lastname, :sirname => sirname, :pizzacrust => pizzacrust, :toppings => toppings}
end

namesex.erb

<link rel="stylesheet" href="/css/pic.css">
<!DOCTYPE html>
<html>
<style>
input[type=text], select {
    width: 100%;
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}

input[type=submit] {
    width: 100%;
    background-color: #4CAF50;
    color: white;
    padding: 14px 20px;
    margin: 8px 0;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

input[type=text]:hover {
    background-color: #45a049;
}

input[type=radio]:hover {
    background-color: #45a049;
}

div {
    border-radius: 5px;
    background-color: #f2f2f2;
    padding: 20px;
}
</style>
<body>

<h3><font color="blue">Zacharys Pizza</h3>

<div>
  <form method="post" action="namesex">
    <label for="firstname">First Name</label>
    <input type="text" id="firstname" name="firstname" placeholder="Your name..">

    <label for="lastname">Last Name</label>
    <input type="text" id="lastname" name="lastname" placeholder="Your last name..">

    <label for="sir name">sir name</label>
    <select id="sirname" name="sirname">
      <option value="mr">mr</option>
      <option value="mrs">mrs</option>
      <option value="miss">miss</option>
    </select>

    <label for="pizza crust">pizza crust</label>
    <br>
    <input type="radio" name="pizzacrust" value="thin"> thin    <input type="radio" name="pizzacrust" value="deep dish"> deep dish
    <br>

    <label for="toppings">toppings</label>
    <br>
    <input type="checkbox" name="toppings" checked="cheese" unchecked=""> cheese <input type="checkbox" name="toppings" value="peporoni"> peporoni <input type="checkbox" name="toppings" checked="sausage" unchecked=""> sausage <input type="checkbox" name="toppings" value="fruit"> fruit
    <input type="hidden" name="toppings" value="nothing">
    <input type="submit">
  </form>
</div>

</body>
</html>

final.erb

<div>
    <%= toppings %>
</div>

1 个答案:

答案 0 :(得分:0)

如果我打开我的repl(我使用pry,IRB也总是可用)并输入以下行,我将得到同样的错误:

"" + nil
TypeError: no implicit conversion of nil into String
from (pry):1:in `+'

第30行的一个(至少)变量是nil

要进行一些简单的调试,请尝试以下方法:

firstname = params[:firstname]
warn "firstname = #{firstname}"

直到找到罪魁祸首。从长远来看,你应该始终确保一个变量不能处于一个它不应该处于的状态,并且它应该到达一个代码的一部分,当它在一个无法使用的状态。

首先,这意味着在变量设置时检查变量,例如

firstname = params.fetch :firstname, "FIRSTNAME"

但这是一个网络应用,所以你不应该为这样的表格设置默认值,除非真的有意,你应该使用HTLM验证和一些javascript验证在表单中添加一些检查。

然后在服务器端app(Sinatra):

error FirstnameNotSet do
  'So what happened was...' + env['sinatra.error'].message
end

# snip…

firstname = params[:firstname]
raise FirstnameNotSet if firstname.nil?

或者您可以根据您是否提供有效信息而有条件返回不同的结果。这种技术适用于AJAX。

firstname = params[:firstname]
# do all the others too
[firstname, lastname].each_with_object([]) do |variable,obj|
   obj << build_json_error_message(variable)
end
if obj.empty?
  erb :pizza
else
  halt 200, {'Content-Type' => 'application/json'}, json obj.join
end

或类似的东西。它不会是那样的,而是沿着这些方向发展的东西。

这应该让你得到解决,并开始采取更好的行动方案。