Mysql2 ::错误:第1行的列'描述'的数据太长

时间:2013-08-27 07:33:25

标签: ruby-on-rails text-editor

我正在使用文本编辑器(wysihtml5)。我在数据库中保存数据时遇到问题。如果我输入的行数超过20行。它显示错误,如“Mysql2 ::错误:第1行的列'描述'的数据太长了。”我正在使用rails4。

在模型中

class News < ActiveRecord::Base
    attr_accessible :name,:published_on,:description

    #associations
     validates :name,           presence: true
     validates :published_on,   presence: true
     validates :description,    presence: true
end

在控制器

class NewsController < ApplicationController
  layout :setting_layout
  before_action :set_news, only: [:show, :edit, :update, :destroy]

  # GET /news
  # GET /news.json
  def index
    @news = News.all
  end

  # GET /news/1
  # GET /news/1.json
  def show
  end

  # GET /news/new
  def new
    @news = News.new
  end

  # GET /news/1/edit
  def edit
  end

  # POST /news
  # POST /news.json
  def create
    @news = News.new(news_params)

    respond_to do |format|
      if @news.save
        format.html { redirect_to @news, notice: 'News was successfully created.' }
        format.json { render action: 'show', status: :created, location: @news }
      else
        format.html { render action: 'new' }
        format.json { render json: @news.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /news/1
  # PATCH/PUT /news/1.json
  def update
    respond_to do |format|
      if @news.update(news_params)
        format.html { redirect_to @news, notice: 'News was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @news.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /news/1
  # DELETE /news/1.json
  def destroy
    @news.destroy
    respond_to do |format|
      format.html { redirect_to news_index_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_news
      @news = News.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def news_params
      params.require(:news).permit(:name, :published_on, :description)
    end
end

在视图中

<%= simple_form_for @news, html: {class: 'form-inline form-horizontal'}, :validate => true do |f|%>
 <p><font color="red">Fields with * are required.</font></p>

  <div class="inputs">
    <%= f.input :name %>
    <%= f.input :published_on, as: 'string', input_html: {class: 'datepicker'} %>
    <%= f.input :description,  as: 'text',   :input_html =>{:rows => '100', :cols => '100', :class => 'input wysihtml5' }%>
  </div>

 <div class="form-actions">
  <%= button_tag(type: 'submit', class: "btn btn-primary") do %>
    <i class="icon-ok icon-white"></i> Save
  <% end %>
</div>  
<% end %>

在Js文件中

$(document).ready(function(){
    $(".wysihtml5").wysihtml5();    
})

在视图中我给出了row和cols值。但它只需要cols值。

enter image description here

1 个答案:

答案 0 :(得分:7)

您可能已在迁移文件中将其设置为string,而不是text string字段被解释为varchar,并且字符长度有限。 text字段不是。