【问题标题】:Nested Forms failing to save rails 4嵌套表单无法保存rails 4
【发布时间】:2014-09-08 00:45:50
【问题描述】:

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

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

待办事项列表标题 --这样会保存

项目 ---这不会保存

控制器

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

名单还在继续。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!每个资源都有不同的答案,我都试过了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多