Не удается обновить форматированный текст в Ruby on Rails
У меня есть модель под названием блог, в которой есть поле с форматированным текстом. Когда я создаю новый пост в блоге, проблем не возникает.
Но когда я пытаюсь обновить существующее сообщение в блоге, богатое текстовое содержимое не обновляется. Но заголовок и изображение обновлены, и рельсы теперь показывают ошибки.
Итак, как мне это решить.
Блоги /new.html.erb
<%= form_for(@blog) do |f| %>
<div class="space" style=" margin-bottom: 20px;">
Company: <%= f.check_box :company %>
</div>
<div class="space">
<%= f.text_field :title, placeholder: "Name", style: "width: 100%;background-color: white; padding-top: 10px; padding-bottom: 10px; padding-left: 10px; margin-bottom: 10px;" %>
</div>
<div class="space">
<%= f.text_field :description, placeholder: "Description", style: "width: 100%;background-color: white; padding-top: 10px; padding-bottom: 10px; padding-left: 10px; margin-bottom: 10px;" %>
</div>
<div class="space" style="">
<%= f.rich_text_area :content, style: "min-height: 500px;" %>
</div>
<div class="space ">
<label class=" button" style="color: #20B2AA; Background-color: white; width: auto; border: solid 1px #20B2AA;padding-left: 20px;padding-right: 20px; padding-top: 10px;padding-bottom: 10px;"id="tasklink">
+ Add Image
<span style="display:none;">
<%= f.file_field :image, class: "custom-file-input", onchange: "javascript:updateList()", id: "file" %>
</span>
</label>
</div>
<div id="fileList" style="margin-bottom: 20px;"></div>
<script>
updateList = function() {
var input = document.getElementById('file');
var output = document.getElementById('fileList');
output.innerHTML = '<ul>';
for (var i = 0; i < input.files.length; ++i) {
output.innerHTML += '<li>' + input.files.item(i).name + '</li>';
}
output.innerHTML += '</ul>';
}
</script>
<div class="button2">
<%= f.submit "Post", class: "btn button", style: "min-width: 25%; padding-top: 10px; padding-bottom: 10px; padding-left: 25px; padding-right: 25px; margin-bottom: 10px; font-size: 1.25em; margin-top:25px;" %>
</div>
<% end %>
Блоги /edit.html.erb
<%= form_for(@blog) do |f| %>
<div class="space" style=" margin-bottom: 20px;">
Company: <%= f.check_box :company %>
</div>
<div class="space">
<%= f.text_field :title, placeholder: "Name", style: "width: 100%;background-color: white; padding-top: 10px; padding-bottom: 10px; padding-left: 10px; margin-bottom: 10px;" %>
</div>
<div class="space">
<%= f.text_field :description, placeholder: "Description", style: "width: 100%;background-color: white; padding-top: 10px; padding-bottom: 10px; padding-left: 10px; margin-bottom: 10px;" %>
</div>
<div class="space" style="">
<%= f.rich_text_area :content, style: "min-height: 500px;" %>
</div>
<div class="space ">
<label class=" button" style="color: #20B2AA; Background-color: white; width: auto; border: solid 1px #20B2AA;padding-left: 20px;padding-right: 20px; padding-top: 10px;padding-bottom: 10px;"id="tasklink">
+ Add Image
<span style="display:none;">
<%= f.file_field :image, class: "custom-file-input", onchange: "javascript:updateList()", id: "file" %>
</span>
</label>
</div>
<div id="fileList" style="margin-bottom: 20px;"></div>
<script>
updateList = function() {
var input = document.getElementById('file');
var output = document.getElementById('fileList');
output.innerHTML = '<ul>';
for (var i = 0; i < input.files.length; ++i) {
output.innerHTML += '<li>' + input.files.item(i).name + '</li>';
}
output.innerHTML += '</ul>';
}
</script>
<div class="button2">
<%= f.submit "Post", class: "btn button", style: "min-width: 25%; padding-top: 10px; padding-bottom: 10px; padding-left: 25px; padding-right: 25px; margin-bottom: 10px; font-size: 1.25em; margin-top:25px;" %>
</div>
<% end %>
config/routes.rb
resources :blog, controller: "blogs" do
resources :blogcomments
end
resources :blogs
get '/blog', :to => 'blogs#index'
Blogs_controller.rb
class BlogsController < ApplicationController
before_action :authenticate_admin!, only: [:create, :new, :update, :edit]
def create
@blog = Blog.new(blog_params)
if @blog.save
redirect_to blog_path(@blog)
end
end
def new
@blog = Blog.new
end
def show
@blog = Blog.friendly.find(params[:id])
@title = @blog.title
if @blog.description
@description = @blog.description
end
@blogs = Blog.all.order('created_at DESC')
@blogcomment = Blogcomment.new
@blogcomments = @blog.blogcomments
end
def edit
@blog = Blog.friendly.find(params[:id])
end
def update
@blog = Blog.friendly.find(params[:id])
if @blog.update_attributes(blog_params)
redirect_to blog_path(@blog)
else
render 'edit'
end
end
def index
@title = "Quantatask Blog"
@description = "The oficial Quantatask blog. Find tips on how to learn to code and on how to start freelancing."
@blogs = Blog.paginate(page: params[:page], per_page: 4).order('created_at DESC')
end
private
def blog_params
params.require(:blog).permit(:title, :description, :content, :company, :image)
end
end
модели /blog.rb
class Blog < ApplicationRecord
has_rich_text :content
has_one_attached :image
extend FriendlyId
friendly_id :title, use: :slugged
has_many :blogcomments
end