1

In one of my forms, I have an element called event_id which value I select from a dropdown box. Once I select event_id, I want to use Ajax to call the controller, and return panel_event_id as a ruby variable that I can use in the rest of the form. Here's what I'm doing:

In routes.rb, I have the following:

match 'panel_event_id', :to => "panels#event_id"

I have the following javascript function (not sure about the syntax):

<script type="text/javascript">
  $('select').change(set_session_event_id);

  function set_session_event_id()
  {
    var event_id = document.getElementById("event_id");
    var event_id_value = event_id.value;
    $.post('panel_event_id', { set_event_id: event_id_value });
  }
</script>

What do I put in my panel_controler.rb, in the following method:

def panel_event_id
end

and how do I act on it back in my view?

1 Answer 1

1

You could render a json in your controller :

def panel_event_id
  ...
  render :json => panel_event.as_json(:only => [ :id ])
end

After that, you could handle the success event of the post method. See the doc here : http://guides.rubyonrails.org/layouts_and_rendering.html and here http://api.jquery.com/jQuery.post/ and here http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.