1

我正在尝试将消息保存到我的活动类,但文本字段未显示。我尝试了一些不同的变化,但似乎无法让它发挥作用。我在 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 不会显示在页面上?

4

2 回答 2

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

于 2012-11-09T07:57:24.893 回答
0

尝试@campaign.build_message新动作或@campaign.messages.new

于 2012-11-09T07:44:26.970 回答