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();    
})

ビューでは、行と列の値を指定しました。ただし、cols値のみを取ります。

ここに画像の説明を入力

4

1 に答える 1

7

stringではなく、移行ファイルでとして設定した可能性がありますtext
stringフィールドは として解釈されvarchar、文字の長さに制限があります。textフィールドではありません。

于 2013-08-27T08:05:55.317 に答える