参数保存不正确(Rails)

我有一个模型消息,我有一个recipient_list保存为字符串。 无论出于何种原因保存,我都会保存除recipient_list之外的所有参数,只剩下recipient_list。 我很难知道这可能是什么原因。

模型:

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"} 

结果:

 #] 

在这种情况下,保存recipient_list的问题可能是什么?

编辑:

Par Ylan的笔记我打算看看它为什么工作,尽管变量名称有所不同。 搞砸了它,我意识到它实际上只是按照这种方式工作,如果我收件人 – >收件人或相反它会停止工作。

摆弄它,并根据纳什的建议提出以下建议:

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

所以现在正在保存recipient_list,我只需要弄清楚如何删除所有不必要的乱码并获得’1’大声笑。 还有什么建议?

编辑#2:添加serialize:recipient_list,Array之后

 #] 

是新的出局,这是我想要的。 我们在这一个上一起工作。 谢谢你们两个。

看起来你应该在你的覆盖编写器中调用super方法:

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

或类似的东西取决于你的代码。

我相信你的写作方法有一个错字。 您正在传递名为recipient的参数,但调用recipients.split(",") 。 更改任何一个,你应该设置。