嵌套表单无法保存导轨4

时间:2014-09-08 00:45:50

标签: ruby forms ruby-on-rails-4 model nested-forms

我有一个TodoList模型和一个TodoItem模型。我可以创建嵌套表单,并在todolist #new中呈现。它只会创造一个todolist头衔。它将接受待办事项列表项,但在索引页面上的项目未显示。 我做了大量的研究,包括文档和这些来源;见帖子的底部。

表单将呈现,没有错误。但是当我点击保存时,待办事项列表标题将保存,但我输入的项目将不会保存。

Todo List Title - 这将保存

产品 ---这不会保存

控制器

class TodoListsController < ApplicationController
  before_filter :authenticate_user!
  before_filter except: [:index]
  before_action :set_todo_list, only: [:show, :edit, :update, :destroy]

  # GET /todo_lists
  # GET /todo_lists.json
  def index
    #@todo_lists = TodoList.all
    @todo_lists = current_user.todo_lists
    @todo_items = current_user.todo_items

  end

  # GET /todo_lists/1
  # GET /todo_lists/1.json
  def show
  end

  # GET /todo_lists/new
  def new
    @todo_list = current_user.todo_lists.new
    @todo_list.todo_items.build
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @todo_list }
    end
  end

  # GET /todo_lists/1/edit
  def edit
    @todo_list = TodoList.find(params[:id])
  end

  # POST /todo_lists
  # POST /todo_lists.json
  def create
    #@todo_list = TodoList.new(todo_list_params)
    @todo_list = current_user.todo_lists.new(todo_list_params)
    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
        format.json { render :show, status: :created, location: @todo_list }
      else
        format.html { render :new }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /todo_lists/1
  # PATCH/PUT /todo_lists/1.json
  def update
    @todo_list = TodoList.find(params[:id])
    respond_to do |format|
      if @todo_list.update(todo_list_params)
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully updated.' }
        format.json { render :show, status: :ok, location: @todo_list }
      else
        format.html { render :edit }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /todo_lists/1
  # DELETE /todo_lists/1.json
  def destroy
    #@todo_list.TodoList.find(params[:id])
    @todo_list.destroy
    respond_to do |format|
      format.html { redirect_to todo_lists_url, notice: 'Todo list was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def owns_todolist
      if current_user != TodoList.find(params[:id]).user
        redirect_to todo_lists_path, error: "You can't do that!"
      end
    end
    # Use callbacks to share common setup or constraints between actions.
    def set_todo_list
      @todo_list = TodoList.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def todo_list_params
      params.require(:todo_list).permit(:title,:todo_list => [:description])
    end
end

模型

 class TodoList < ActiveRecord::Base
        has_many :todo_items, dependent: :destroy
        accepts_nested_attributes_for :todo_items

    end


    class TodoItem < ActiveRecord::Base
        belongs_to :todo_list

    end

表格

<%= form_for(@todo_list) do |f| %>
  <% if @todo_list.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@todo_list.errors.count, "error") %> prohibited this todo_list from being saved:</h2>

      <ul>
      <% @todo_list.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>

  <%= f.fields_for :todo_items do |b| %> 
   <--- I also tried passing this in fields for @todo_list.todo_items.build -- >
    <%= b.label :description, "Items" %><br />
    <%= b.text_field :description %>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

以前的研究

Nested Form with Nested Resource Rails 4

Rails 4 Nested Form.

How to Create Nested Forms in Rails 4

http://www.createdbypete.com/articles/working-with-nested-forms-and-a-many-to-many-association-in-rails-4/

http://railscasts.com/episodes/196-nested-model-form-revised?view=comments

Rails 4 Nested form fields not saving in database

这个名单还在继续。

1 个答案:

答案 0 :(得分:1)

问题在于你的todo_list_params方法。它应该是这样的

def todo_list_params
 params.require(:todo_list).permit(:title,todo_items_attributes: [:description])
end