Martijn Kerckhaert
Martijn Kerckhaert

Reputation: 494

jQuery .replacewith ruby code

I have ruby code that calls a method to accept a candidate for a vacancy like this:

<%= link_to "Accept", accept_candidate_path(vacancy_id: @vacancy.id, employee_id: e.id), method: :post, :remote => true, :id => "link_#{i}" %>

The method changes the state in the database to Accepted.

Once that is clicked, I want to replace that with following ruby code:

<%= link_to "Cancel", back_to_pending_path(vacancy_id: @vacancy.id, employee_id: e.id), method: :post, :remote => true%>

I also have a if/else that checks the state of a candidate to display either links. But that is only triggered by refreshing the page and I just want it to accept it, change the button without completely refreshing the page.

Currently I have this:

$("[id^='link_']").click(function (event) {
  $(this).replaceWith("Accepted")
});

and it works completly fine with just plain text, but I need the text to be the embedded ruby. Is this possible?

Upvotes: 0

Views: 400

Answers (1)

Deepak Mahakale
Deepak Mahakale

Reputation: 23671

it works completly fine with just plain text, but I need the text to be the embedded ruby. Is this possible?

You have to create .js files for your action where you can change the html of link with paths as well

e.g create accept_candidate.js.erb and add respond_to js format it will do the trick

# accept_candidate.js.erb
$("[id^='link_']").html("<%= escape_javascript(render('cancel_link')) %>")


# _cancel_link.html.erb
<%= link_to "Cancel", back_to_pending_path(vacancy_id: @vacancy.id, employee_id: e.id), method: :post, :remote => true%>

Upvotes: 1

Related Questions