bitdon
bitdon

Reputation: 9

How to render a partial based on specific link clicked?

I'm having trouble rendering partials using Ajax/jQuery in my Rails app and am wondering where i'm going wrong.

Rails 5.2.0

On my Dashboard view, i'm trying to render partials based on the specific sidebar link item clicked.

In my views/users/my_dashboard.html.erb:

<li class="sidenav__list-item">
    <%= link_to "Transaction History", transaction_path, :remote: true %>
</li>

My partial _test.html.erb:

<div>
  <p>It works if this shows</p>
</div>

and in my views/users/my_dashboard.js.erb:

$('#render-test').replaceWith("<%= j render 'users/partials_dashboard/test' %>");

In my views/users/my_dashboard.html.erb, When the Transaction History link is clicked, i need it to render its corresponding partial:

// replace this div, with the rendered partial

<div id="render-test">
</div>

Controller users_controller.rb:

def my_dashboard
    respond_to do |format|
      format.html
      format.js
    end
end

What am i missing?

Upvotes: 0

Views: 95

Answers (1)

Xavier
Xavier

Reputation: 4017

You can not generate ruby with Javascript, the ruby won't be interpreted. If you really need to something dynamic, then you should consider creating an API route and then, call this API with Jquery

Upvotes: -1

Related Questions