form_for中的rails参数

时间:2011-09-26 13:34:29

标签: ruby-on-rails

我正在制作更新或保存已保存邮件的表单。

<% form_for @draft, :url => {:controller => "drafts", :action => "draft_actions"} do |f|%>

      subject
  <%=f.text_field :subject %>

      recipients
  <%=f.text_field :draft_recipients %>

 <br /> <%= f.text_area :body %>


      <%= submit_tag "save", :name => "resave_draft"%>
      <%= submit_tag "send", :name => "send_draft" %>

<% end %>

但我希望收件人以逗号显示,以逗号分隔:user1, user2, user3而不是user1user2user3 我的问题是:我不明白如何在我的草稿模型中将:recipients符号映射到我的收件人属性,但仍然有显示收件人的视图显示我想要的方式

我的draft模型的代码是

class Draft < ActiveRecord::Base

belongs_to :message
belongs_to :draft_recipient, :class_name => "User"
delegate :created_at, :subject, :user, :body, :draft_recipients,  :to => :message

我的消息模型的代码如下

class Message < ActiveRecord::Base

belongs_to :user
has_many :recipients, :through => :message_copies
has_many :draft_recipients, :through => :drafts
has_many :message_copies
has_many :drafts, :class_name => "Draft", :foreign_key => :message_id

attr_accessor :to #array  of people to send to
attr_accessible :subject, :body, :to, :recipients, :author, :user

我确信有一种相当简单的方法可以做到这一点,但我无法理解它。

1 个答案:

答案 0 :(得分:4)

<%=f.text_field :draft_recipients, :value => @draft.draft_recipients.join(',') %>

可能会为你工作。但是,您是否在控制器中处理转换?提交时,这不会完全正确绑定。