I'm building a Chrome extension that needs to post to a Rails app.
I can send the request with Fetch but the JSON object isn't right when Rails processes the request. My controller returns an error:
NoMethodError (undefined method permit' for #<String:0x00007ff83b5a36e8>)
The logs say the data object looks like this:
{"highlight"=>"{\"text\":\"hello world!\",\"url\":\"helloworld.com\"}"}
The data object should look something like this:
{"highlight"=>{"text"=>"hello world!", "url"=>"helloworld.com"}}
For some reason, Rails processes the request as a string with the quotes escaped, and not a hash. I don't know how to pass it from Fetch so that it doesn't do that.
Here is the JavaScript code:
function captureHighlight(selection, tab) {
const highlight = {
text: selection.selectionText,
url: selection.pageUrl
}
return highlight;
}
async function postHighlight(highlight) {
let formData = new FormData();
formData.append('highlight', JSON.stringify(highlight));
const response = await fetch('http://localhost:3000/highlights', {
method: 'POST',
body: formData,
});
console.log(response);
}