ruby - Rails 4: link_to submit form using ajax, how to imitate post request? -


in rails app want use able hide on wall doing ajax request , adding item id database.

here request want imitate following:

started post "/report_fixed_vulns" 127.0.0.1 @ 2013-08-19 21:28:45 +0100 processing reportfixedvulnscontroller#create html   parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "report_fixed_vuln"=>{"report_id"=>"2", "vuln_id"=>"2", "user_id"=>"2"}, "commit"=>"create report fixed vuln"} 

i tried using following code this.

<%= button_to 'submit', report_fixed_vuln_path(:report_fixed_vuln => {:report_id => @report_id, :vuln_id => plugin.first.id, :user_id => current_user.id}), :remote => true, :method => :put %> 

however generates different request:

started put "/report_fixed_vulns/323?report_fixed_vuln%5breport_id%5d=323&report_fixed_vuln%5buser_id%5d=1&report_fixed_vuln%5bvuln_id%5d=443" 127.0.0.1 @ 2013-08-19 21:32:51 +0100 processing reportfixedvulnscontroller#update js   parameters: {"authenticity_token"=>"xxx", "report_fixed_vuln"=>{"report_id"=>"323", "user_id"=>"1", "vuln_id"=>"443"}, "id"=>"323"} 

here question: how imitate first request using button_to (the parameters don't need entered -- they're there on page)

you should refer routing guide:

http://guides.rubyonrails.org/routing.html

it says helper create method is:

post    /admin/posts    create  admin_posts_path 

so should use helper report_fixed_vulns_path (note plural) same arguments method: :post (put update), in case:

<%= button_to 'submit', report_fixed_vulns_path(:blabla => hash), :remote => true, :method => :post %> 

if want hide button right after clicked on it, can add onclick event: (jquery)

<%= button_to 'submit', report_fixed_vulns_path(:blabla => hash), :remote => true, :method => :post, :onclick => '$(this).hide();' %> 

Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -