Question

I'm trying to have a message saved to my campaign class but the text field isn't showing up. I've tried a few different variations but can't seem to get it to work. I put a debugger in the fields_for and the code doesn't even get down to that logic. Here's the code:

  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'

why wouldn't the text_area be showing on the page?

Était-ce utile?

La solution

Try @campaign.build_message in new action or @campaign.messages.new

Autres conseils

for your solution do this, and try it

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

and in your model file, write

accepts_nested_attributes_for :message

For more Info - http://railscasts.com/episodes/196-nested-model-form-part-1

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top