form_for 中的 rails 参数 [英] rails parameters in form_for
问题描述
我正在制作用于更新或保存已保存消息的表单.
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 %>
但我希望以一种很好的方式显示收件人,用逗号分隔:user1, user2, user3
而不是 user1user2user3
我的问题是:我不明白如何将 :recipients
符号映射到我的草稿模型中的收件人属性,但仍然有显示收件人的视图显示我想要的方式
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 recipient attribute in my draft model, but still have the view displaying the recipients displayed how I want
我的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
我的消息模型的代码如下
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.
这篇关于form_for 中的 rails 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!