Using Rails 4.2 and Draper gem. I have a decorator:
def status_link
if enabled?
h.link_to 'Disable', h.disable_company_path(id), data: {confirm: 'Are you sure?'}, method: :put, remote: true
else
h.link_to 'Enable', h.enable_company_path(id), method: :put, remote: true
end
end
I want to render the link in js view:
var cell = $("#my-id");
cell.html(<%= @my_model.decorate.status_link %>);
But the link is not injected in the cell. The cell is empty.
If I print in console the status_link method it works:
<%= pp @my_model.decorate.status_link %>
"<a data-confirm=\"Are you sure?\" data-remote=\"true\" rel=\"nofollow\" data-method=\"put\" href=\"/companies/210/disable\">Disable</a>"
If I try to inject an integer it works:
cell.html(<%= @my_model.id %>);
Also I have try to use double quote:
cell.html("<%= @my_model.decorate.status_link %>");
But the result is the same. I have try to inspect the code with the browser console but no errors are shown
Why the link is not injected while integer works fine?
The following is invalid:
You need to call the javascript function with a string, i.e.:
The takeaway point here should not be memorising this specific error, but rather, learning how to debug your code.
Navigate to the relevant page in your application and open the browser console. Now perform whatever action should trigger that javascript - such as a page refresh, or clicking a button. You should see an error in the log; something like this:
This also includes a link to the line of code that the error originated from - i.e. in your case, the
cell.html(<%=....code.