文本框的字段不使用嵌套属性显示

时间:2012-11-09 07:42:51

标签: ruby-on-rails fields-for

我正在尝试将邮件保存到我的广告系列类,但文本字段未显示。我尝试了一些不同的变化,但似乎无法让它工作。我在fields_for中放了一个调试器,代码甚至没有达到那个逻辑。这是代码:

  def new
    @campaign = Campaign.new
    @message = Message.new(:campaign_id => @campaign.id)
  end

  def edit
    @campaign = Campaign.find(params[:id])
    @message = @campaign.message
  end

  %legend
    Enter the campaign information
  .field
    .control-group
      %label.control-label
        Campaign Name
      .controls
        = f.text_field :name

        = f.collection_select(:group_id, current_user.groups.all, :id, :name, :include_blank => true)
        = f.fields_for :message do |m|
          = m.text_area :body, :rows => 3
      .form-actions= f.submit "#{params[:action] == 'new' ? 'Create New Campaign' : 'Save Campaign'}", :class => 'btn btn-success'

为什么text_area不会显示在页面上?

2 个答案:

答案 0 :(得分:0)

在新操作中尝试@campaign.build_message@campaign.messages.new

答案 1 :(得分:0)

为您的解决方案执行此操作并尝试

def new
  @campaign = Campaign.new
  @message = @campaign.build_message
end

并在您的模型文件中写下

accepts_nested_attributes_for :message

了解更多信息 - http://railscasts.com/episodes/196-nested-model-form-part-1