将参数传递给rails 3中的before_create回调
因此,受本教程的启发,我正在为 Facebook 的社交网络平台实现一个消息系统 http:// www.novawave.net/public/rails_messaging_tutorial.html
我已经具备发送消息的基本功能,现在我希望用户能够保存草稿以便稍后发送。
创建消息时,它会在消息模型的 before_create 方法中为每个收件人创建该消息的副本。
to.each do |recipient|
recipient = User.find(recipient)
message_copies.build(:folder_id => recipient.inbox.id, :recipient_id => recipient.id, :read => false)
end
我想将参数传递给 before_create 回调,以便它知道是否应该为收件人构建消息副本,或者只是将其保存为草稿,这样我就可以使用相同的方法而无需更改模型。
我想过跳过这种特殊情况的回调,但不明白如何做到这一点。
这是创建新消息的视图
<% form_for @message, :url => {:控制器=> “已发送”,:动作=> “创建”}做|f| %>
<select name="message[to][]" multiple="multiple">
<%= options_from_collection_for_select(User.find(:all), :id, :login, @message.to) %>
</select>
<p>
Subject: <%= f.text_field :subject %>
</p>
<p>
Body:
<br />
<%= f.text_area :body %>
</p>
<p>
<%= submit_tag "Send", :name => 'do_send' %>
<%= submit_tag "Save", :name => 'save_draft' %>
</p>
<%结束%>
有什么建议吗?
谢谢
编辑:更多代码!
So i am implementing a messaging system à la Facebook for a social network platform inspired by this tutorial http://www.novawave.net/public/rails_messaging_tutorial.html
I have the basic funcionality of sending messages working and now I want the user to be able to save a draft for sending it later.
when creating a message it creates copies of this message for each recipient in the before_create method of my message model.
to.each do |recipient|
recipient = User.find(recipient)
message_copies.build(:folder_id => recipient.inbox.id, :recipient_id => recipient.id, :read => false)
end
I would like to pass parameters to the before_create callback so that it knows if it should build message copies to the recipients or simply save it as a draft, so I can use the same method without altering the model.
I thought of skipping callbacks for this particular case but couldn't understand how to do it.
Here is the view for creating a new message
<% form_for @message, :url => {:controller => "sent", :action => "create"} do |f| %>
<select name="message[to][]" multiple="multiple">
<%= options_from_collection_for_select(User.find(:all), :id, :login, @message.to) %>
</select>
<p>
Subject: <%= f.text_field :subject %>
</p>
<p>
Body:
<br />
<%= f.text_area :body %>
</p>
<p>
<%= submit_tag "Send", :name => 'do_send' %>
<%= submit_tag "Save", :name => 'save_draft' %>
</p>
<% end %>
Any suggestions?
thanks
edit : more code!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可以借助 Ajax 将消息和消息的接收者存储在数据库中。这样您就可以像 gmail 一样继续发送消息。
另一种选择是在表单上对“发送”和“另存为草稿”进行 2 个不同的提交,并根据提交参数,您可以处理消息。
I think you can store the message and the receivers of the message with the help of Ajax in the database. That way you can continue with the message after wards similar to gmail.
Another option is to have 2 different submits for "Send" and "Save as Draft" on the form and based on commit parameter you can process the message.
您可能有条件回调
和虚拟属性之类的东西来决定是构建发送给收件人的消息副本还是简单地将其保存为草稿。
You might have something like conditional callbacks
and a virtual attribute to decide either build message copies to the recipients or simply save it as a draft.