2013-10-01 156 views
0

我正在使用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 %> 
+0

我首先想到的是,強的參數被過濾掉相關的屬性。嘗試在create方法的頂部插入它來檢查參數的內容:'render text:topic_params and return'。 – mysmallidea

+0

{「name」=>「Test」,「description」=>「讓我們再試一次」,「forum_id」=>「3」} 是的,它甚至沒有觸及這部分:posts_attributes:[:id,:內容] – Lindsiria

+1

在您的視圖中,我看到的字段爲:回覆但不是:帖子。你確定你在params [:topic]中看到一個名爲posts_attributes的參數嗎? – davidfurber

回答

0

感謝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