# Fetching a partial on click

**URL:** <https://discuss.hotwired.dev/t/fetching-a-partial-on-click/1297>\
**Category:** General\
**Created:** [July 27, 2020, 2:37am UTC](https://discuss.hotwired.dev/t/fetching-a-partial-on-click/1297 "2020-07-27T02:37:31Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![welearnednothing](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/welearnednothing/32/502_2.png) [@welearnednothing](https://discuss.hotwired.dev/u/welearnednothing)\
**Post date:** [July 27, 2020, 7:04pm UTC](https://discuss.hotwired.dev/t/fetching-a-partial-on-click/1297/4 "2020-07-27T19:04:35Z")

</div>

I’d say this is a great approach. In my experience it’s very common. As I mentioned, it can be done in such a way that the Stimulus controller is generic and reusable, and the backend code is obviously trivial, so it’s win win all around.

You can simplify the Stimulus controller even more if you leverage Rails’ Unobtrusive Javascript to make the actual calls to the controller by leveraging `remote: true` on your links.

For example:

**index.html.erb**

```auto
<ul>
<% @customers.each do |customer| %>
  <li data-controller="renderer">
    <%= link_to customer.name, customer_detail_path(customer), data: { remote: true, action: 'ajax:success->renderer#render' } %>
    <span data-target="renderer.display"></span>
  </li>
<% end %>
</ul>

```

**renderer\_controller.js**

```auto
import { Controller } from 'stimulus'

export default class extends Controller {
  static targets = ['display']

  render(evt) {
    this.displayTarget.innerHTML = evt.detail[0].body.innerHTML
  }
}

```

You’ve effectively introduced a single line of JS here that can be reused in many situations by leveraging the [JS that’s already packaged in Rails](https://guides.rubyonrails.org/working_with_javascript_in_rails.html). It’s an all around pretty elegant solution if you ask me!

One thing I’ll point out, I did reference a `customer_detail_path` here, hinting at using a `CustomerDetailsController` rather than bundling the details response in with the `CustomersController`. Purely optional, but it follows [this advice from DHH](http://jeromedalbert.com/how-dhh-organizes-his-rails-controllers/) and I’ve found it to be helpful in keeping my controllers dead simple.

---

_[View the full topic](https://discuss.hotwired.dev/t/fetching-a-partial-on-click/1297)._
