I have a Rails 4 form that builds a list of parts using AJAX on the form page. Once the list of parts is built in the
I want to submit the list as an
You have to remember Rails doesn't care about HTMl, so when you mention you want to send "
list as params", it's got nothing to do with the
, but how you're porting those items into Rails
Routes
The most robust way to do this is to use a route, and pass the params through JS to it:
#config/routes.rb
post "your_route(/:items)", to: "controller#action"
#app/views/parts/index.html.erb
- 1
- 2
- 3
<%= link_to "Submit", class: "submit_link" %>
#app/assets/application.js.erb
$("a.submit_link").on("click", function() {
var data = jQuery('#serv_parts_list li').map(function(){
return 'id[]=' + this.id.match(/(\d+)$/)[1]
}).get()
$.ajax ({
url: "your_route",
data: data.join('&')
success: function(data) {
//success
},
error: function(data) {
//error
}
});
});