Ckeditor无法保存textarea值(Ruby on Rails)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ckeditor无法保存textarea值(Ruby on Rails)相关的知识,希望对你有一定的参考价值。
我正在尝试使用Ckeditor在我的网站上发布博客文章。但是,当我通过Ckeditor创建帖子时,不保存文本区域值。 (当我使用rails c时,我没有传递值的问题。)
Started POST "/posts" for 219.124.107.202 at 2018-10-30 09:37:18 +0000
Processing by PostsController#create as html
Parameters: {"utf8"=>"✓", "authenticity_token"=>"I5cHP2V1MLzQ3+WOZGpLbtw+UT0h3gb9Wc6LxVi+hrkBJrMs0q4OMw0L6tX1VXny3pQy8ieb7hWAQ4z5gdxVvQ==", "post"=>{"title"=>"Error", "body"=>"<p>there is an error</p>
"}, "commit"=>"Create Post"}
(0.1ms) begin transaction
↳ app/controllers/posts_controller.rb:17
Post Create (1.5ms) INSERT INTO "posts" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2018-10-30 09:37:18.007504"], ["updated_at", "2018-10-30 09:37:18.007504"]]
↳ app/controllers/posts_controller.rb:17
(4.9ms) commit transaction
↳ app/controllers/posts_controller.rb:17
Redirected to https://c92406fe48e44359a9072aa73bfcde1c.vfs.cloud9.us-east-2.amazonaws.com/posts/2
Completed 302 Found in 12ms (ActiveRecord: 6.5ms)
编辑:我忘记在控制器文件中上传代码。这是下面的'posts_controller.rb'。
class PostsController < ApplicationController
before_action :find_post, only: [:edit, :update, :show, :delete]
def index
@posts = Post.all
end
def new
@post = Post.new
end
def create
@post = Post.new
if @post.save(post_params)
flash[:notice] = "Successfully created post!"
redirect_to post_path(@post)
else
flash[:alert] = "Error creating new post!"
render :new
end
end
def edit
end
def update
if @post.update_attributes(post_params)
flash[:notice] = "Successfully updated post!"
redirect_to post_path(@posts)
else
flash[:alert] = "Error updating post!"
render :edit
end
end
def show
end
def destroy
if @post.destroy
flash[:notice] = "Successfully deleted post!"
redirect_to posts_path
else
flash[:alert] = "Error updating post!"
end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
def find_post
@post = Post.find(params[:id])
end
end
Edit2:这是“帖子”的视图。
'_form.html.erb':
<%= simple_form_for @post do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2>
<%= "#{pluralize(@post.errors.count, "error")} prohibited this post from being saved:" %>
</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.input :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.input :body, :as => :ckeditor, input_html: {:ckeditor => {:toolbar => 'FULL'}}, class: "form-control" %>
</div>
<div class="form-group">
<%= f.button :submit %>
</div>
<% end %>
yedit.html.erby:
<h2>Edit Post</h2>
<%= render "posts/form" %>
'new.html.erb':
<h2>Create New Post</h2>
<%= render "posts/form" %>
'show.html.erb':
<div class="col-sm-11 col-xs-12 blog-content">
<h2 class="text-center"><%= @post.title %></h2>
<h5 class="text-center"><%= @post.created_at.strftime('%b %d, %Y') %></h5>
<div><%= raw @post.body %></div>
</div>
'index.html.erb':
<div class="container">
<div class="col-sm-10 col-sm-offset-1 col-xs-12 blog-content">
<% @posts.each do |post| %>
<div class="col-xs-12">
<div class="text-center">
<h2><%= post.title %></h2>
<h6><%= post.created_at.strftime('%b %d, %Y') %></h6>
</div>
<div>
<%= raw(post.body).truncate(358) %>
</div>
<div class="text-center">
<%= link_to "READ MORE", post_path(post) %>
</div>
<br>
</div>
<% end %>
</div>
</div>
答案
create
的posts_controller
行动有错误。我已经更新了你的create
动作
def create
@post = Post.new(post_params)
if @post.save
flash[:notice] = "Successfully created post!"
redirect_to post_path(@post)
else
flash[:alert] = "Error creating new post!"
render :new
end
end
我希望它现在能够奏效......
以上是关于Ckeditor无法保存textarea值(Ruby on Rails)的主要内容,如果未能解决你的问题,请参考以下文章
如何初始化 CKEditor textarea 换行符,就像 html textarea 换行符一样
来自ckeditor textarea的ajax表单提交值之后没有通过邮件发送
从 WYSIWYG CKEditor 传递 textarea 值