ruby-on-rails 服务器错误:第1行中列"描述"的数据太长

mgdq6dx1  于 2023-02-17  发布在  Ruby
关注(0)|答案(2)|浏览(115)

我正在使用文本编辑器(wysihtml5)。我在数据库中保存数据时遇到问题。如果我键入了超过20行。它会显示错误,如"Myql2::Error:第1行的列"description"的数据太长。我正在使用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的值。

6rqinv9w

6rqinv9w1#

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

dgtucam1

dgtucam12#

如果字段为text,则将其更改为longtext,例如

change_column :your_table, :your_column, :longtext

相关问题