3

我正在使用文本编辑器(wysihtml5)。我在将数据保存在数据库中时遇到问题。如果我输入了超过 20 行。它显示类似“Mysql2::Error: Data too long for column 'description' at row 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 值。

在此处输入图像描述

4

1 回答 1

7

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

于 2013-08-27T08:05:55.317 回答