0

In my application, the user clicks on one of three choices. When the user clicks on the choice, I want to grab that choice and then use a popup to display more information about it. However, I want to use a controller to make an api call based on the user choice so that I can get more data about the choice they chose.

How can I create a function in my controller to do just this?

1
  • @rkon I can create the popup. I just don't know how to manipulate the data in a controller function. I am at a loss as to where to indicate that the partial is being controlled by a certain controller function Commented May 14, 2014 at 7:01

1 Answer 1

1

If you want to render a partial I suppose you want to call the controller through AJAX. I would solve this by adding remote: true to the links representing the three choices, and setting up the actions in the controller accordingly.

For instance:

<%= link_to 'choice 1', choice_path, class:'choice-link', data: { choice: 1 }, remote: true %>.

# controller
def choice_action
  # some filter based on which choice (1, 2 or 3) the user chose
  render partial: 'path/to/partial', layout: false if request.xhr?
end

The layout flag indicates that you just return the HTML generated by the partial, and do not wrap it in the application layout you normally would when rendering server-generated HTML templates.

In order to append the HTML to your DOM I'd use a listener on the choice links, for instance like so:

$('.choice-link').on 'ajax:success', (data, status, xhr) ->
  $('#popup').html(data)
Sign up to request clarification or add additional context in comments.

2 Comments

Ok. I tried this, and I am getting a response, but the ajaxsuccess is not working.
@DavidMckee: Please note the colon -> ajax:success. It's also important that you bind the handler on the element which triggers the ajax call (or its parent).

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.