0

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?

3
  • 1
    You have quite a number of options here. 1) show/hide both links and just toggle them depending. i.e.: show accept; when click accept, hide accept and show cancel. 2) add a accept.js.erb file and add the javascript here. "Unobtrusive javascript" - edgeguides.rubyonrails.org/… 3. Handle the link via AJAX in a javascript file and return a partial (via controller) with the new link. it'll take some time to type all these up with examples, but I will if you would like. Commented Oct 7, 2015 at 15:35
  • 1
    use a data atribute on the elements and read that value Commented Oct 7, 2015 at 15:37
  • 1
    Yep, I should have included that in my list @charlietfl. And to add the to data-attribute, you could even group the controller methods into one and accept/cancel depending on current state, so you wouldn't have to change much client side. Commented Oct 7, 2015 at 15:37

1 Answer 1

1

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%>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer, one small issue there, I am getting the employee_id in a loop in my view and thus I get the error undefined local variable or method `e' for #<#<Class:0x007fdd840e2ab8>:0x007fdd83ca80b0> with the parameters employer_id and vacancy_id, and I need the employee_id aswell to target the correct row to change.
you need to set @employee object from accept/candidate action that so that it will be available in your views and then change e.id to @employee.id
Works like a charm! Thanks!

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.