嵌套对象不会保存[Rails 4]
问题描述:
我正在使用Rails构建一个论坛。嵌套对象不会保存[Rails 4]
当您创建主题时,它将具有Topic类中的Name和Description以及Post类中的Post内容...(当您创建一个线程时,您将自动创建一个包含内容的帖子)
含义: 对于每个主题,您可以有多个帖子 对于每个帖子,您必须有一个主题。
由于某些原因,当我创建主题时,它会保存主题的输入,但会抛出Post类的输入。
我环顾了整个Stackoverflow,发现了一些类似的问题,并尝试了很多答案,只是为了得到错误或没有任何改变。 (accepts_nested_attributes_for not saving the child attribute to the database,
模型
class Topic < ActiveRecord::Base
belongs_to :forum
has_many :posts, :dependent => :destroy
belongs_to :user
accepts_nested_attributes_for :posts
end
class Post < ActiveRecord::Base
belongs_to :topic
end
控制器 - 主题
class TopicsController < ApplicationController
before_action :set_topic, only: [:show, :edit, :update, :destroy]
def index
@topics = Topic.all
end
def new
@topic = Topic.new
responses = @topic.posts.build
end
def create
@topic = Topic.new(topic_params)
if @topic.save
flash[:success] = "Topic Posted"
redirect_to "/forums/#{@topic.forum_id}"
else
render :new
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_topic
@topic = Topic.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def topic_params
params.require(:topic).permit(:name, :description, :last_poster_id, :last_post_at, :forum_id,
posts_attributes: [:id, :content])
end
end
_form
<%= form_for(@topic) do |f| %>
<% if params[:forum] %>
<input type="hidden"
id="topic_forum_id"
name="topic[forum_id]"
value="<%= params[:forum] %>" />
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<%= f.fields_for :responses do |p| %>
<%= p.label :content %><br />
<%= p.text_area :content %>
<% end %>
<%= f.submit :class => "btn btn-primary" %>
<% end %>
答
感谢davidfurber,我弄清楚了什么是错的。如果其他人有同样的问题,这是解决方案。
您不需要将@ topic.posts.build分配给变量。这就是说,你不需要_form视图中的响应。相反,把:帖子:答复是。
_form
<%= form_for(@topic) do |f| %>
...
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<%= f.fields_for :posts do |p| %>
<%= p.label :content %><br />
<%= p.text_area :content %>
<% end %>
<%= f.submit :class => "btn btn-primary" %>
<% end %>
控制器 - 主题
class TopicsController < ApplicationController
before_action :set_topic, only: [:show, :edit, :update, :destroy]
def new
@topic = Topic.new
@topic.posts.build
end
def create
# render text: topic_params and return
@topic = Topic.new(topic_params)
if @topic.save
flash[:success] = "Topic Posted"
redirect_to "/forums/#{@topic.forum_id}"
else
render :new
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_topic
@topic = Topic.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def topic_params
params.require(:topic).permit(:name, :description, :last_poster_id, :last_post_at, :forum_id,
posts_attributes: [:id, :content])
end
end
我首先想到的是,强的参数被过滤掉相关的属性。尝试在create方法的顶部插入它来检查参数的内容:'render text:topic_params and return'。 – mysmallidea
{“name”=>“Test”,“description”=>“让我们再试一次”,“forum_id”=>“3”} 是的,它甚至没有触及这部分:posts_attributes:[:id,:内容] – Lindsiria
在您的视图中,我看到的字段为:回复但不是:帖子。你确定你在params [:topic]中看到一个名为posts_attributes的参数吗? – davidfurber