Passing Rails form input as string
I have a rails form that uses a GET request to submit data. I have a select_tag o开发者_开发技巧ption, which essentially gives the user a list of options where they can select multiple. This data is then submitted through the URL as an array, which is very ugly and long. Is there a way to instead call a join and submit this data as a string?
Thanks!
What you can do is create a hidden input field and then take over form submission in javascript and pull the data from your select field, do your join magic and store that data in a hidden field.
For example, in HAML:
= form_tag your_path, :id=> "your-form", :method => :post do
= select_tag "your-options", "<option>Option1</option><option>Option2</option><option>Option3</option>"
%input(type="hidden" id="select_data" name="select_data" value="")
:javascript
$(document).ready(function() {
$('#your-form').submit(function(){
data = getDataFromYourSelectFieldAndJoinItIntoAString();
//store in hidden field
$('#select_data').val(data);
//clear options selection
$('#your-options').val('');
return true;
});
});
精彩评论