Skip to content Skip to sidebar Skip to footer

Passing Javascript Variable To Ruby-on-rails Controller

How can I pass a variable (id) from the JavaScript listener: Gmaps.map.callback = function() { ... ... google.maps.event.addListener(marker, 'click', function() {

Solution 1:

If I am understanding correctly, a good way to do this is to use AJAX to submit the id to your action and use the action to render your form.

That would look something like this in your javascript:

jQuery.ajax({
  data: 'id=' + id,
  dataType: 'script',
  type: 'post',
  url: "/controller/action"
});

You'll need a route:

post 'controller/action/:id' => 'controller#action'

Then in your action you can grab that id and render your form something like this:

defaction@user = User.relationships.build(:followed_id => params[:id])
  render :viewname, :layout => falseend

Then you can just build a form for @user in a partial or whatever

<%= form_for @userdo |f| %>

You'll have to fill in the details there, but that should get you pretty close.

Post a Comment for "Passing Javascript Variable To Ruby-on-rails Controller"