Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Processing data with jQuery Ajax and Rails


  • Please log in to reply
1 reply to this topic

#1 Tronjer

Tronjer

    Passenger

  • Members
  • 1 posts

Posted 16 November 2013 - 07:46 PM

Hi there,

 

Coming from PHP, I currently try to learn Rails but got stuck on Ajax as Ruby doesn't know superglobals like $_POST.

 

Let's say I have an index.html.erb

<div id="box"></div>
<p><%= image_tag("button.png", class: "trigger_button", ajax_path: mymodule_test_path, alt: "Button") %></p>

<script>
$('.trigger_button').on('click', function() {
  $.ajax({
    url: $(this).attr('ajax_path'),
    type: 'POST',
    data: { foo : 'bar' },
    success: function(result) {
      $('#box').html(result);
    }
  });
})
</script>

What I want to achieve is sending data to an url which processes post['foo'] and renders the result into index.html. In PHP this could look like:

<? $baz = $_POST['foo']; ?>
<p><? echo $baz; ?></p>

But how to solve this in RoR?



#2 Kevin

Kevin

    Signalman

  • Members
  • 21 posts

Posted 21 November 2013 - 08:55 AM

use params, in your case that would be :



params[:foo]







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users