Submit
    list as parameter array in Rails 4 form, adding params values to params hash using javascript

后端 未结 2 945
后悔当初
后悔当初 2021-01-07 11:57

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
2条回答
  •  -上瘾入骨i
    2021-01-07 12:40

    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 } }); });

提交回复
热议问题