嵌套对象不会保存[Rails 4]

时间:2013-10-01 21:49:41

标签: ruby-on-rails ruby-on-rails-4 nested-attributes

我正在使用Rails构建一个论坛。

当你创建一个主题时,它将具有主题类的名称和描述,以及来自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 %>

1 个答案:

答案 0 :(得分: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