本文介绍了参数未正确保存(导轨)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个消息模型,为此,我有一个另存为字符串的收件人列表.无论出于何种原因保存,我的所有参数(收件人列表除外)都将被保存,仅收件人列表被忽略.我为此感到困惑.

I have a model Messages, for which I have a recipient_list which saves as a string. For whatever reason on save, all of my parameters other than the recipient_list are being saved, with only the recipient_list being left out. I'm stumped as to what the cause for this may be.

型号:

class Message < ActiveRecord::Base
  attr_accessible :content, :sender_id, :recipient_list
  attr_reader :recipient_list #necessary for jquery-token-input

  belongs_to :sender, class_name: "User"

  validates :content, presence: true
  validates :sender_id, presence: true
  validates :recipient_list, presence: true

  def recipient_list=(recipient) #jquery-token-input
    self.recipient_ids = recipients.split(",")
  end

end

控制器:

def create
  @message = current_user.sent_messages.build(params[:message])
  if @message.save
    flash[:success] = "Message Sent."
    redirect_to '/users/'+current_user.id.to_s+'/messages'
  else
    redirect_to '/users/'+current_user.id.to_s+'/messages'
  end
end

参数:

{"utf8"=>"✓",
"authenticity_token"=>"WlStV4ogguSX72vrZp10zJbucS5MTL1pT1DLt06qjcw=",
"message"=>{"recipient_list"=>"1,2",
"content"=>"foobar123",
"sender_id"=>"1"},
"commit"=>"Send"}

结果:

#<Message id: 32, content: "foobar123", sender_id: 1, recipient_list: "", created_at: "2012-08-22 19:38:44", updated_at: "2012-08-22 19:38:44">]

在这种情况下,保存收件人列表的问题可能是什么?

What might be the problem that is keeping the recipient_list from being saved in this case?

Par Ylan的注释我着手看看为什么变量名称不同但它为什么能工作.弄乱它之后,我意识到,实际上,只有当我使收件人成为收件人->收件人时,它才可以正常工作.否则,它将停止工作.

Par Ylan's note I set out to see why it was working despite the difference in variable name.upon messing with it, I realized that it actually was only working that way if i made recipient -> recipients or the reverse the it would stop working.

对此进行了修饰,并根据Nash的建议提出了以下建议:

Fiddled with it, and based on Nash's suggestion came up with the following:

def recipient_list=(ids)
 recipient_list = ids.split(",")
 super(recipient_list)
end


#<Message id: 42, content: "foobar123", sender_id: 1, recipient_list: "---\n- '1'\n", created_at: "2012-08-22 21:58:46", updated_at: "2012-08-22 21:58:46">]

因此,现在保存了receive_list,我只需要弄清楚如何删除所有不必要的乱码并仅获取'1'大声笑.还有其他建议吗?

So now the recipient_list is being saved, I just have to figure out how to remove all the unecessary garble and get just the '1' lol. Any further suggestions?

编辑#2:添加后 serialize:recipient_list,阵列

Edit #2:After adding serialize :recipient_list, Array

#<Message id: 43, content: "foobar123", sender_id: 1, recipient_list: ["1", "2"], created_at: "2012-08-22 22:10:46", updated_at: "2012-08-22 22:10:46">]

是我想要的新输出.我们在这一方面共同努力.谢谢两个.

is the new out put which is what i was going for. We worked together on this one. Thanks you two.

推荐答案

看起来您应该在重写的编写器中调用super方法:

looks like you should call super method in your overriden writer:

def recipient_list=(recipients) #jquery-token-input
  self.recipient_ids = recipients.split(",")
  super(recipients)
end

或类似的内容取决于您的代码.

or something similar depends on your code.

这篇关于参数未正确保存(导轨)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:55