【问题标题】:how to display all checked check boxes instead of just the last one selected如何显示所有选中的复选框,而不仅仅是最后一个选中的复选框
【发布时间】:2018-09-24 10:06:12
【问题描述】:

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>

【问题讨论】:

  • 另外,请花点时间阅读stackoverflow.com/help/asking,因为我知道您在几个小时前曾问过此问题的重复或几乎重复。

标签: html css ruby sinatra erb


【解决方案1】:

如果我打开我的 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"

但这是一个 Web 应用程序,因此您不应该为这样的表单设置默认值,除非真的有意,您应该在表单中添加一些使用 HTLM 验证和一些 javascript 验证的检查。

然后在服务器端应用程序(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

或者类似的东西。它不会是那样的,而是类似的东西。

这应该可以帮助您解决问题并开始采取更好的行动。

【讨论】:

  • 好吧,我感谢您的帮助,我将所有支票簿的名称都相同,然后从那里移动,但我怎样才能让它显示所有选中的选项,而不是最后一个在我的最终选择.erb
  • @zacharyedgell 使用屏幕截图编辑您的问题,显示您拥有什么以及您想要什么。
  • 我将代码更新为现在的样子并上传了图片,并更改了描述和标题
  • warn "toppings = #{toppings.inspect}" 的输出是什么?这将告诉您应用程序是否正确接收所有复选框数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 2014-11-30
  • 1970-01-01
  • 2012-04-20
相关资源
最近更新 更多