wachichornia
wachichornia

Reputation: 1188

how to make js the format of a rails form submitted via ajax?

I have this form:

<div id='relationship_<%= relationship.id %>'>
  <% @value_id = "value_#{relationship.id}" %>
  <% @div_slider_id = "slider_value_#{relationship.id}" %>
  <% @form = "edit_relationship_#{relationship.id}"%>
  <%= form_for(relationship, :remote => true) do |f| %>
    <div id="<%= @div_slider_id %>"></div>
    <%= f.hidden_field :value, :id => "#{@value_id}" %>
  <% end %>
</div>

<script type='text/javascript'>
$(document).ready( function() {
  $(function() {
    $( "#<%= @div_slider_id %>" ).slider({
      orientation: "horizontal",
      range: "min",
      value: <%= relationship.value || 1 %>,
      min: 0,
      max: 100,
      change: function( event, ui ) {
        $( "#<%= @value_id %>" ).val( ui.value );

If the user moves the slider, the form is sent as HTML, not JS

        $("#<%= @form %>").submit();
      }
    });
    $( "#<%= @value_id %>" ).val( $( "#<%= @div_slider_id %>" ).slider( "value" ) );
  });
});
</script>

How do I sent that form on JS format when I move the slider?

thanks

Upvotes: 2

Views: 1520

Answers (2)

Anthony Alberto
Anthony Alberto

Reputation: 10405

Add an ID to the form submit :

<%= f.submit 'Ready', :id => "button_submit" %>

Then in JS instead of this :

$("#<%= @form %>").submit();

Do

$("#buttom_submit").click();

Or : try adding a data-type to the form declaration :

<%= form_for(relationship, :remote => true, :html => { "data-type" => :js } ) do |f| %>

Upvotes: 2

ddb
ddb

Reputation: 1406

  1. Set relationship_form id to form

2. Change this line in your code :

$("#<%= @form %>").submit();

to

$js.post("/relationship.js", $js("#relationship_form").serialize());

Upvotes: 0

Related Questions