I am making a form for updating or saving a saved message.
<% 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 %>
but I want the recipients to be displayed in a nice way, separated by commas : user1, user2, user3
instead of user1user2user3
My problem is : I don't understand how to have the :recipients
symbol map to my 开发者_StackOverflow中文版recipient attribute in my draft model, but still have the view displaying the recipients displayed how I want
The code for my draft
model is
class Draft < ActiveRecord::Base
belongs_to :message
belongs_to :draft_recipient, :class_name => "User"
delegate :created_at, :subject, :user, :body, :draft_recipients, :to => :message
The code for my Message model is the following
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
I'm sure there's a fairly straightforward way to do that, but I can't get my head around it.
<%=f.text_field :draft_recipients, :value => @draft.draft_recipients.join(',') %>
Might work out for you. However, are you handling the conversion back in the controller? This won't bind exactly right when you submit.
精彩评论