【问题标题】:fields for text box not appearing using nested attributes文本框的字段未使用嵌套属性出现
【发布时间】:2012-11-09 07:42:50
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails fields-for


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      为您的解决方案执行此操作,然后尝试一下

      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

      【讨论】:

        猜你喜欢
        • 2020-08-29
        • 1970-01-01
        • 1970-01-01
        • 2015-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多