Petr
Petr

Reputation: 2099

How to pass span id to rails controller?

I have this very simple star-rating system. I am getting the value that has been chosen by html span:

<span id="count"></span>

I am wondering how I can pass the value of the span to my Rails controller:

class FeedbacksController < ApplicationController
    def new
    end

    def create
    end
end

How can I pass the span ID either in rails form or by AJAX? Would love to see both examples if possible. Thank you!

Thanks!

Upvotes: 1

Views: 227

Answers (1)

Pardeep Dhingra
Pardeep Dhingra

Reputation: 3946

You can use ajax call to post data to your controller action. You don't need to send span id to your controller, instead on change of rating you can call ajax.

$('#stars').on('starrr:change', function(e, value){
   $("#count").html(value);

   $.ajax({
    type: 'POST',
    url: "/url_you_want_to_call",
    data: { count: value},
    dataType: "text",
    success: function(data) { alert("Save Complete") }
  });
});

Upvotes: 1

Related Questions