Sinatra 中的动态表单

发布于 2024-11-02 11:37:14 字数 5070 浏览 1 评论 0原文

在自学 Sinatra 时,我想制作一个 Web 应用程序,通过 ICMP 监控设备并在浏览器中显示结果。

我有一个配置页面,用户可以在其中决定他想要监控的设备数量以及这些设备的 IP 和描述。我希望表单是动态的,这样我就可以轻松地迭代变量。我不想预测用户想要跟踪多少台设备。我想要这样的东西。

<form name="config" action="/config" method="post">
#of devices to check: <select name="numofdevices">
<% case $devices.to_i
  when 1 %>
    <option value=1 SELECTED>1</option>
    <option value=2>2</option>
    <option value=3>3</option>
<% when 2 %>
    <option value=1>1</option>
    <option value=2 SELECTED>2</option>
    <option value=3>3</option>
<% when 3 %>
    <option value=1>1</option>
    <option value=2>2</option>
    <option value=3 SELECTED>3</option>
<% end %>
</select> <br />
<% $devices.times do |devicenum| %>
     Ip Address: <input type="text" name='<%= devicenum %>[ip]'> Description: <input type="text" name='<%= devicenum %>[desc]'><br />
<% end %>

显然这行不通,因为哈希符号以字符串的形式出现。我必须通过

deviceOne = params[:"1"]

我无法迭代

$numofdevices.times do |num|
   devices[num] = params[:num.to_s]
end

这个变量来访问变量主要是我想将所有这些设备放入一个易于访问的数组中。我不想提前为用户可能跟踪的设备数量做好准备。我希望代码能够计算他们选择的设备数量并相应地填充数组。

最终我想访问这样的设备。在哈希数组中。

$devices[0][:ip]

然后我可以轻松地遍历每个设备。

$devices.each do |device|
   puts "IP: #{device[:ip]}, Desc: #{device[:desc]}"
end

我想到了这样的事情,

$numofdevices.times do |devicenum|
   devices[devicenum] = params[:devicenum]
end

我确信我做错了什么,而且我对 ruby​​ 哈希/数组如何工作的了解一定是有限的。我希望有一种更好的方法来做到这一点,而不是下面的方法。

<form name="config" action="/config" method="post">
#of devices to check: <select name="numofdevices">
    <% case $devices.to_i
      when 1 %>
        <option value=1 SELECTED>1</option>
        <option value=2>2</option>
        <option value=3>3</option>
    <% when 2 %>
        <option value=1>1</option>
        <option value=2 SELECTED>2</option>
        <option value=3>3</option>
    <% when 3 %>
        <option value=1>1</option>
        <option value=2>2</option>
        <option value=3 SELECTED>3</option>
    <% end %>
    </select> <br />
    <% case $devices.to_i
       when 1 %>
            Ip Address: <input type="text" name='firstd[ip]'> Description: <input type="text" name='firstd[desc]'><br />
    <% when 2 %>
            Ip Address: <input type="text" name='firstd[ip]'> Description: <input type="text" name='firstd[desc]'><br />
            Ip Address: <input type="text" name='secondd[ip]'> Description: <input type="text" name='secondd[desc]'><br />
    <% when 3 %>
            Ip Address: <input type="text" name='firstd[ip]'> Description: <input type="text" name='firstd[desc]'><br />
            Ip Address: <input type="text" name='secondd[ip]'> Description: <input type="text" name='secondd[desc]'><br />
            Ip Address: <input type="text" name='thirdd[ip]'> Description: <input type="text" name='thirdd[desc]'><br />
    <% when 4 %>
            Ip Address: <input type="text" name='firstd[ip]'> Description: <input type="text" name='firstd[desc]'><br />
            Ip Address: <input type="text" name='secondd[ip]'> Description: <input type="text" name='secondd[desc]'><br />
            Ip Address: <input type="text" name='thirdd[ip]'> Description: <input type="text" name='thirdd[desc]'><br />
            Ip Address: <input type="text" name='fourthd[ip]'> Description: <input type="text" name='fourthd[desc]'><br />
    <% end %>

然后,如果用户决定使用那么多设备,我就必须预先创建设备变量以准备接受数据。我的路线看起来像这样。

post '/config' do
  @title = "Config"
  $devices = params[:numofdevices].to_i
  $firstdevice   = params[:firstd]
  $seconddevice  = params[:secondd]
  $thirddevice   = params[:thirdd]
  $fourthdevice  = params[:fourthd]
  erb :config
end

我确信我做错了。如果我想跟踪大量设备,这会变得重复且麻烦。我是一个学习新手。有人能指出我正确的方向吗? :)

[编辑]

我将在我的帖子“/config”中尝试以下内容

params.keys.each do |key|
  @objects.push(params[key])
end

[编辑编辑]

这是我能想到的最好的。

在我的配置 erb

<% $numofdevices.times do |id| %>
        Ip Address: <input type="text" name='<%= id %>[ip]'> Description: <input type="text" name='<%= id %>[desc]'><br />
    <% end %>

在我的帖子“配置”

params.keys.each do |key|
    $devices.push(params[key]) if params[key].include?("ip")
  end

中现在我不必担心符号的命名方式或它们的命名方式。 :p

不过,如果有人有任何其他方法可以做到这一点,那就更好了,我将不胜感激! :)

In teaching myself Sinatra I wanted to make a web app that monitored devices via ICMP and displayed the results in browser.

I have a config page where the user decides how many devices he wants to monitor and what the IP and description of those devices are. I want the form to be dynamic so I can iterate through the variables easily. I don't want to have to anticipate how many devices the user wants to track. I want something like this.

<form name="config" action="/config" method="post">
#of devices to check: <select name="numofdevices">
<% case $devices.to_i
  when 1 %>
    <option value=1 SELECTED>1</option>
    <option value=2>2</option>
    <option value=3>3</option>
<% when 2 %>
    <option value=1>1</option>
    <option value=2 SELECTED>2</option>
    <option value=3>3</option>
<% when 3 %>
    <option value=1>1</option>
    <option value=2>2</option>
    <option value=3 SELECTED>3</option>
<% end %>
</select> <br />
<% $devices.times do |devicenum| %>
     Ip Address: <input type="text" name='<%= devicenum %>[ip]'> Description: <input type="text" name='<%= devicenum %>[desc]'><br />
<% end %>

Obviously this won't work though because the hash symbol comes in the form of a string. I would have to access a variable via

deviceOne = params[:"1"]

I could not iterate through this

$numofdevices.times do |num|
   devices[num] = params[:num.to_s]
end

Mainly I want to throw all of these devices into an easily accessible array. I don't want to have to prepare for the number of devices the user might track ahead of time. I want the code to count the number of devices they selected and populate an array accordingly.

Ultimately I want to access a device like so. In an array of hashes.

$devices[0][:ip]

Then I could iterate through each devices easily.

$devices.each do |device|
   puts "IP: #{device[:ip]}, Desc: #{device[:desc]}"
end

SOmething like this came to mind

$numofdevices.times do |devicenum|
   devices[devicenum] = params[:devicenum]
end

I know for sure I am doing something wrong and that my knowledge of how ruby Hashes/arrays work must be limited. I was hoping there was a better way of doing this rather then the following.

<form name="config" action="/config" method="post">
#of devices to check: <select name="numofdevices">
    <% case $devices.to_i
      when 1 %>
        <option value=1 SELECTED>1</option>
        <option value=2>2</option>
        <option value=3>3</option>
    <% when 2 %>
        <option value=1>1</option>
        <option value=2 SELECTED>2</option>
        <option value=3>3</option>
    <% when 3 %>
        <option value=1>1</option>
        <option value=2>2</option>
        <option value=3 SELECTED>3</option>
    <% end %>
    </select> <br />
    <% case $devices.to_i
       when 1 %>
            Ip Address: <input type="text" name='firstd[ip]'> Description: <input type="text" name='firstd[desc]'><br />
    <% when 2 %>
            Ip Address: <input type="text" name='firstd[ip]'> Description: <input type="text" name='firstd[desc]'><br />
            Ip Address: <input type="text" name='secondd[ip]'> Description: <input type="text" name='secondd[desc]'><br />
    <% when 3 %>
            Ip Address: <input type="text" name='firstd[ip]'> Description: <input type="text" name='firstd[desc]'><br />
            Ip Address: <input type="text" name='secondd[ip]'> Description: <input type="text" name='secondd[desc]'><br />
            Ip Address: <input type="text" name='thirdd[ip]'> Description: <input type="text" name='thirdd[desc]'><br />
    <% when 4 %>
            Ip Address: <input type="text" name='firstd[ip]'> Description: <input type="text" name='firstd[desc]'><br />
            Ip Address: <input type="text" name='secondd[ip]'> Description: <input type="text" name='secondd[desc]'><br />
            Ip Address: <input type="text" name='thirdd[ip]'> Description: <input type="text" name='thirdd[desc]'><br />
            Ip Address: <input type="text" name='fourthd[ip]'> Description: <input type="text" name='fourthd[desc]'><br />
    <% end %>

I would then have to pre-create device variables ready to accept data should the user decide to use that many devices. MY route looks like this.

post '/config' do
  @title = "Config"
  $devices = params[:numofdevices].to_i
  $firstdevice   = params[:firstd]
  $seconddevice  = params[:secondd]
  $thirddevice   = params[:thirdd]
  $fourthdevice  = params[:fourthd]
  erb :config
end

I am certain I am doing this wrong. If I want to track a large number of devices this becomes repetitive and cumbersome. I'm a learning newb. Can anyone out there point me in the right direction? :)

[EDIT]

I'm going to try the following in my post '/config'

params.keys.each do |key|
  @objects.push(params[key])
end

[EDIT EDIT]

Here is the best I have been able to come up with.

In my config erb

<% $numofdevices.times do |id| %>
        Ip Address: <input type="text" name='<%= id %>[ip]'> Description: <input type="text" name='<%= id %>[desc]'><br />
    <% end %>

In my post 'config'

params.keys.each do |key|
    $devices.push(params[key]) if params[key].include?("ip")
  end

Now I don't have to worry what the symbols get named or how they are named. :p

Still if anyone has any other ways of doing this that would be even better I'd appreciate the input! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提笔书几行 2024-11-09 11:37:14

这是我能想到的最好的。

在我的配置 erb

<% $numofdevices.times do |id| %>
        Ip Address: <input type="text" name='<%= id %>[ip]'> Description: <input type="text" name='<%= id %>[desc]'><br />
    <% end %>

在我的帖子“配置”

params.keys.each do |key|
    $devices.push(params[key]) if params[key].include?("ip")
  end

中不,我不必担心符号的命名方式或它们的命名方式。 :p

不过,如果有人有任何其他方法可以做到这一点,那就更好了,我将不胜感激! :)

Here is the best I have been able to come up with.

In my config erb

<% $numofdevices.times do |id| %>
        Ip Address: <input type="text" name='<%= id %>[ip]'> Description: <input type="text" name='<%= id %>[desc]'><br />
    <% end %>

In my post 'config'

params.keys.each do |key|
    $devices.push(params[key]) if params[key].include?("ip")
  end

No I don't have to worry what the symbols get named or how they are named. :p

Still if anyone has any other ways of doing this that would be even better I'd appreciate the input! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文