我有兩個模型一個Topic和Topic_Content。嵌套屬性不會保存在數據庫中
用下面的代碼
路線
resources :topics do
resources :topic_contents
end
主題
class Topic < ActiveRecord::Base
has_one :topic_content
accepts_nested_attributes_for :topic_content
end
TopicContent
class TopicContent < ActiveRecord::Base
belongs_to :topics
end
控制器
class TopicsController < ApplicationController
def new
@topic = Topic.new
end
def create
# render text: params[:topic].inspect
@topic = Topic.new(topic_params)
@topic.save
end
private
def topic_params
params.require(:topic).permit(:title, topic_content_attributes: [:text])
end
end
查看
<%= form_for @topic do |f| %>
<%= f.label 'Topic:' %>
<%= f.text_field :title %>
<%= f.fields_for :topic_contents do |tf| %>
<%= tf.label :text %>
<%= tf.text_area :text %>
<% end %>
<%= f.submit %>
<% end %>
的冠軍將在主題表保存正確的,但topic_content(文本)將不會保存在數據庫,我找不到問題。
Thx,快速的回答它幫助我很多!一個問題留給我。它設置外鍵正確,但**:文本**不是。 – dot
問題已解決 – dot