本文介绍了Rails选择多个选项只发送一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< form accept-charset =UTF-8action =/ contracts / <%= params [:id]%> / addConsultantsmethod =POSTid =updateConsultants><!---->
< div style =margin:0; padding:0; display:inline>
< input name =utf8type =hiddenvalue =✓>
< input name =authenticity_tokentype =hiddenvalue =<%= session [:_ csrf_token]%>>
< / div>
< div class =consultant_selector>
< h2>顾问< / h2>
< option value =<%= u [:id_user]%>><%= u [:name]%>< / option>
<%end%>
< / select>
< ul>
< li class =triangle_rightid =add_consultant>< / li>
< li class =triangle_leftid =remove_consultant>< / li>
< / ul>
< / select>
< / div>
< div>
< input type =submitvalue =Mettreàjour/>
< / div>
< / form>
而在我的控制器中:
def add_consultants
@contract = Contract.find(params [:id])
@consultants = params [:consultants_selected];
puts------ Consultants:#{@ consultants}
redirect_to contract_path
end
我的想法是,我可以从第一个选择中选择姓名,点击三角形将其移动到第二个,然后提交表单以将这些姓名分配给合同。然而,当我这样做时,对 puts >的调用仅打印出第二个select中的3个ID(它们都有选定的属性为 true )。如何获得多个值?
解决方案
刚刚在这里找到答案:
更改第二个选择的名称:
< select name =consultants_selectedsize =10multiple>
< / select>
收件人:
< select name =consultants_selected []size =10multiple>
< / select>
已处理。
I have this form :
<form accept-charset="UTF-8" action="/contracts/<%= params[:id] %>/addConsultants" method="POST" id="updateConsultants"><!----> <div style="margin:0;padding:0;display:inline"> <input name="utf8" type="hidden" value="✓"> <input name="authenticity_token" type="hidden" value="<%= session[:_csrf_token] %>"> </div> <div class="consultant_selector"> <h2>Consultants</h2> <select name="consultants_available" size="10" multiple> <% @users.each do |u| %> <option value="<%= u[:id_user] %>"><%= u[:name] %></option> <% end %> </select> <ul> <li class="triangle_right" id="add_consultant"></li> <li class="triangle_left" id="remove_consultant"></li> </ul> <select name="consultants_selected" size="10" multiple> </select> </div> <div> <input type="submit" value="Mettre à jour" /> </div> </form>
And in my controller :
def add_consultants @contract = Contract.find(params[:id]) @consultants = params[:consultants_selected]; puts "------ Consultants: #{@consultants}" redirect_to contract_path end
The idea is that I can select names from the first select, move them to the second by clicking on the triangle and then submit the form to assign these names to a contract. However, when I do this the call to puts only prints one ID out of the 3 I have in the second select (all three of them have the selected attribute to true). How do I get multiple values?
解决方案
Just found the answer here : https://stackoverflow.com/a/2196522/1796742
Changing the name of the second select from:
<select name="consultants_selected" size="10" multiple> </select>
To :
<select name="consultants_selected[]" size="10" multiple> </select>
Worked.
这篇关于Rails选择多个选项只发送一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!