我有一个名为 blog 的模型,它有一个富文本字段。创建新博客文章时没有问题。
但是当我尝试更新现有的博客文章时,富文本内容没有得到更新。但是标题和图像已更新,并且 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 %>
配置/路由.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