获取复选框的值 [Ruby/Sinatra(Rails)]
我在一个视图上有一些复选框,我想在另一个视图上进行评估,但我不明白它在做什么。
我读过帖子/博客,说明了 name
的不同方法:-
update_params[] # array
update_params[0], update_params[1] # known indexed array
update_params0, update_params1 # differently named
因此,使用第一个似乎最常见的方法:-
# views/index.erb
<input type="checkbox" name="update_params[]" value="Copy" />Update the host<br/>
<input type="checkbox" name="update_params[]" value="Start" />Start the software<br/>
value
应该被索引吗? 0
,1
?
所以我想要两个结果
1)在 version.erb 视图上显示从 index.erb 视图中选择的选项。
所以它看起来像:-
Copy : Yes
Start : No
目前我有:-
# views/version.erb
<p>Copy : <%= params['update_params[0]'] %></p>
<p>Start : <%= params['update_params[1]'] %></p>
2)评估传递给脚本的选项,以便它们成为命令行选项,即 -c
、-l
所以我的“控制器”
# update.rb
helpers do
def run_update(version, host, params)
command = "./update.sh #{params} #{host}" # -c -l
@ok = system( command )
end
end
post '/version' do
run_update(params[:version_list], params[:host], params[:update_params])
erb :version
end
I have some checkboxes on one view, that I want to eval on another, but I dont understand what its doing.
I've read posts/blogs stating different approaches to the name
:-
update_params[] # array
update_params[0], update_params[1] # known indexed array
update_params0, update_params1 # differently named
So going with the first which seems the most common :-
# views/index.erb
<input type="checkbox" name="update_params[]" value="Copy" />Update the host<br/>
<input type="checkbox" name="update_params[]" value="Start" />Start the software<br/>
Should the value
be indexed? 0
, 1
?
So I want two outcomes
1) Display the options selected from the index.erb view on the version.erb view.
So that it looks something like :-
Copy : Yes
Start : No
Currently I have :-
# views/version.erb
<p>Copy : <%= params['update_params[0]'] %></p>
<p>Start : <%= params['update_params[1]'] %></p>
2) eval the options to pass to a script so that they become command line options, ie -c
, -l
So my "controller"
# update.rb
helpers do
def run_update(version, host, params)
command = "./update.sh #{params} #{host}" # -c -l
@ok = system( command )
end
end
post '/version' do
run_update(params[:version_list], params[:host], params[:update_params])
erb :version
end
也许您可以使用数组中的键而不是依赖索引?例如:
那么,你可以做这样的事情:
Maybe instead of relying on index, you could use keys in the array? For example:
So then, you can do stuff like: