Reputation: 1180
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?
Upvotes: 0
Views: 103
Reputation: 2270
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)
Upvotes: 1