I'm trying to make the contents of a <div contenteditable="true">
saved to the db in my Rails app through a form, so it can be saved and updated by the user.
Here is the setup:
The user gets a view (i.e /posts/1) that includes a <div>
with contenteditable= "true"
. I added the javacript to give me a 'Save' button and a hidden field that will capture the content of the div for saving, as suggested in Can div with contenteditable=true be passed through form?. Then changed it to follow the suggestions in this same question.
<%= form_for @post do |form| %>
<%= form.hidden_field :content %>
<%= form.submit "Save" %>
<% end %>
<div id="content" contenteditable="true">
<p>Hello</p>
</div>
<script>
document.getElementById('content').addEventListener('input', function(ev){
document.getElementById('post_content').value = ev.target.innerHTML;
})
</script>
The behavior I'm looking for is for this form to, once I press 'Save', save all the contents of <div id="content">
to @post.content
I've added :content
to my post_params
in posts_controller.rb
def post_params
params.require(:post).permit(:user_id, :content)
end
But it doesn't work. @post.content
stays as nil
as ever.
Server logs shows that the param :content is being sent, but empty!
Started PATCH "/posts/3" for ::1 at 2019-10-01 19:36:24 +0200
Processing by PostsController#update as HTML
Parameters: {"authenticity_token"=>"SRzcfcrBWOCdaWeJkp6rCRw94Ex2XLuVvnc3Bt1dFP1JFz36NCNTcxcuOVqzymlIGq9sveaE/+OkDnaHv3kJNQ==", "post"=>{"content"=>""}, "commit"=>"Save", "id"=>"3"}
Any guesses of what am I doing wrong? Thanks in advance