2

I looking for the solution to use two submit buttons with one form like the following:

  1. The first should submit the given form, to the create function in my controller
  2. The second should execute the given form remotely, to show the entered text under the form as preview

View:

<%= form_for @topic do |f| %>
 .................
 ... some code ...
 .................
  <p>
    <%= f.fields_for :topic_content do |tf| %>
    <%= tf.text_area  :text , :id => 'topic_text',  :cols => 100 , :rows => 15, :class => 'topic_text' %></p>
  <% end %>
 .................
 ... some code ...
 .................
  <%= f.submit "Save", :name => 'save' %>
  <%= f.submit "Preview", :name => 'preview' %>
<%end%>

<div id='preview_topic_text'>
</div>
dot
  • 486
  • 1
  • 8
  • 17
  • I'm little bit confused how should that in **rails** work? – dot Apr 27 '14 at 15:06
  • Simply integrate the javascript code in your erb in a ` – Uri Agassi Apr 27 '14 at 15:39
  • 2
    The duplicate question is on a complete other topic, namely on `target`-attributes, instead of having one regular (save) and one 'Ajax' button (preview). – Veger Jan 06 '15 at 23:08

2 Answers2

2

Please change your submit to link_to tag and give id for that link_tag,then write ajax function

<%=link_to('Save', '#', :class => "save","data-button" => "save") %>

for preview

<%=link_to('preview', '#',:class => "save","data-button" => "preview") %>
jQuery(".save").click(function(){
      var form_value = jQuery('#your_form_id').serialize();  
      var button = $(this).data("button");     
      if(button == "save"){
          //your ajax code
          // in data you append button the value
      }
      else{

      }
     // document.multi.action+="?"+form_value+"&flag="+ button;
     // document.multi.submit();

});

so,in controller you can get params[:flag],then you could check the condition.

Jenorish
  • 1,694
  • 14
  • 19
-3

u can use PHP to define conditions of what button is pressed, try this code:

 if (isset($_GET['submitbtn1']))
    {
       //execute the 1st code here

    }
 else if (isset($_GET['submitbtn2']))
    {
       //execute the 2nd code here

    }