Saturday 15 May 2010

forms - Rails: Submit button outside form_tag -


I am using a button to process in the set of selected buttons. This solution is based on Railscast # 165.

It's all working well, but only if the submit_tag button is within the form (which is a table in my application). I want to put the submit button in the page header, but it breaks the form's connection.

How do I put a submit button outside the form's body?

  & lt;% = form_tag select_cosmics_path, method :: put% & gt; & Lt;% = submit_tag "Accept Check" ,: class = & gt; "BTN BTN-Primary"%> & Lt; Table class = "table-striped" & gt; . . & Lt;% @ cosmics.each do | Cosmic & Gt%; & Lt; TR & gt; & Lt; Td> & Lt;% = check_box_tag "cosmic_ids []", cosmic.id% & gt; & Lt; / Td> . . & Lt; / TR & gt; & Lt;% end% & gt; & Lt; / Table & gt; & Lt;% end% & gt;   

routes.rb

  Resources: Collect the Universe: Select End and End    

You can create a button outside the form, and when the button is clicked, submit the form Use Javascript:

HTML:

  & lt; Button ID = "myBtn" class = "BTN BTN-Primary" & gt; Checked & lt; / Button & gt; ... & lt;% = form_tag select_cosmics_path, method :: put, id: 'myForm' do% & gt; ... & lt;% end% & gt;   

Javascript (using jQuery):

  $ (document) .ready (function () {$ ('# myBtn'). On ( 'Click', function () {$ ('# myForm'). Submit ();});});   

You can add javascript directly to the onClick attribute of the button.

No comments:

Post a Comment