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]'> De开发者_开发技巧scription: <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! :)
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! :)
精彩评论