Ckeditor无法保存textarea值(Ruby on Rails)

时间:2018-10-30 09:55:58

标签: ruby-on-rails ckeditor

我正在尝试实施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>\r\n"}, "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 %>

' edit.html.erb'

<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>

1 个答案:

答案 0 :(得分:0)

create的{​​{1}}动作有误。我已经更新了您的posts_controller操作

create

我希望它现在可以工作...