mysql - 删除存储在 MySQL 中的文本的额外附加字符

标签 mysql ruby-on-rails ruby

我有一个 api Controller ,它从 api 用户那里收集信息。但是,某些用户信息在发送和存储在 MySQL 数据库中时有时会显示添加下划线,例如 "this is a test ________" 而不是 this is a test。然而,当通过浏览器运行时,它存储得很好。

可能是什么问题。

Controller 摘录;

@message.message = CGI.unescape(params[:message]).strip

作为临时修复,任何想法我都可以删除在存储中或消息到达时添加到消息中的所有 6 个下划线。

class Api::V1::Json::MessagesController < ApplicationController
before_filter :authenticate
require 'uri'
require 'cgi'
def sms
    @message = Message.new
    #@message.to = decoded_to.gsub(/[^\d]/,"")
    @message.to = CGI.unescape(params[:to]).strip.gsub("+","").gsub(/\s+/, "")
    @message.from =  CGI.unescape(params[:from])
    @message.message = CGI.unescape(params[:message]).strip
    @message.user_id = current_user.id
    @message.status = 'Queued'
    if @message.save
        MessageWorker.perform_async(@message.id, [""], current_user.id)
        render json: {status: "Success"} 
    else
        render json: {status: "Failed" }
    end
end

private

def authenticate
  error!('Unauthorized. Invalid token.', 401) unless current_user
end

def current_user
  # find token. Check if valid.
  user_token = params[:token]
  token = ApiKey.where(:access_token => user_token).first
  if token 
    @current_user = User.find(token.user_id)
  else
    false
  end
end
end

模型是;

class Message < ActiveRecord::Base
 attr_accessible :message, :phone, :status, :to, :from, :user_id
 attr_accessor :schedule
 validates :message, :presence => true
 validates :from, :presence => true
 validates :to, :presence => true
 validates :status, :presence => true
 validates_length_of :message, :maximum => 1600, :allow_blank => true
 validates_length_of :from, :maximum => 11, :allow_blank => false
 belongs_to :user

最佳答案

像这样改变你的模型

class Message < ActiveRecord::Base

  before_save :strip_underscore

  def strip_underscore
   self.message.gsub("______","")
  end

end

关于mysql - 删除存储在 MySQL 中的文本的额外附加字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19633454/

相关文章:

ruby - 在 Ruby 中将数据从一列转换为多列

ruby-on-rails - 如何将参数发送到回形针处理器

ruby - Selenium-Webdriver:找到元素后获取属性

php - MySQL 数据库在条目中没有转义单引号...如何显示它们?

php - 查询 DROP mysql 动态触发

ruby-on-rails - 具有多种形式的CSRF token

ruby-on-rails - 思考 sphinx rails 不运行

php - 在 MySQL 表中插入 XML 解析数据时出现问题

mysql - 更新查询不影响数据库中的所有行

ruby-on-rails - postgres 中的 Ruby/Rails 字符串大小写比较问题