我有一个如下所示的控制器方法。但是,schedule = params[:message][:schedule]
接受这样的输入["","2"]
并作为字符串存储在 MySQL 数据库中。
当Schedule.create(execution_time: scheduled_time, lists: "lists", message_id: @message.id, user_id: current_user.id)
存储的值是数据库"--- \n- \"58\"\n- \"\"\n"
时,Schedule.create(execution_time: scheduled_time, lists: "#{lists}", message_id: @message.id, user_id: current_user.id)
然后存储的值是这样"[\"34\", \"\"]"
的,但所需的值是["34", ""]
。
控制器方法如下;
def create
@lists = current_user.lists.all
@message = Message.new(params[:message])
lists = params[:message][:lists]
schedule = params[:message][:schedule]
if @message.save
if schedule.blank?
MessageWorker.perform_async(@message.id, lists, current_user.id)
else
scheduled_time = DateTime.strptime(schedule,"%m/%d/%Y %H:%M %p %z").to_datetime.utc.strftime("%Y-%m-%d %H:%M:%S")
Schedule.create(execution_time: scheduled_time, lists: "#{lists}", message_id: @message.id, user_id: current_user.id)
end
redirect_to new_message_path, notice: 'Message was successfully added to the queue.'
else
render action: "new"
flash[:notice] = "Messages Not Sent"
end
为什么存储值中有斜杠?先感谢您。