中尝试
我试图将消息保存到我的广告系列中,但文本字段未显示。我尝试了几种不同的变体,但似乎无法使它起作用。我在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不在页面上显示?
在新操作或@campaign.messages.new
@campaign.build_message
为您的解决方案执行此操作,然后尝试
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