Rails通过表单传递表单ID参数

时间:2017-08-04 16:19:02

标签: ruby-on-rails forms routes ruby-on-rails-5

我是rails的新手,我正在构建一个待办事项列表应用。我试图设置它 - 像Trello,所以我可以输出多个列表,并在每个列表下有项目。所以目前我可以创建列表,并在每个列表中创建列表项。一切都在通过在不同的页面之间引导玩家而工作,但我试图将它们全部合并到一个页面中。

以下是我试图了解每个列表的模拟图像:

Mock up of Final for Each list

我大部分时间都在工作。我可以创建新列表,它们会自动显示在页面上。如果我添加项目,则项目将显示在每个列表下方。这是我到目前为止所拥有的图像:

Current Progress

我遇到问题的最后一件事包括能够在每个列表中的索引页面中创建列表项。如果您查看上面的模拟图像,列表顶部会显示一个文本字段。我希望做同样的事情并为该列表创建一个新项目。

我收到此错误“TodoLists #index中的NoMethodError”。我理解为什么它出错了,因为索引动作不知道todo_items但是无法弄清楚如何使它工作。

这是我的页面索引页面,我想要发生一切:

               <div class="row wrapper border-bottom white-bg page-heading">

      <div class="col-lg-10">
        <h2>Todo Lists</h2>
        <ol class="breadcrumb">
          <li>
            <a href="index.html">Home</a>
          </li>
          <li>
            <a>Todo Lists</a>
          </li>
          <li class="active">
            <strong>Lists</strong>
          </li>
        </ol>
      </div>
      <div class="col-lg-2"></div>
    </div>

    <div class="wrapper wrapper-content  animated fadeInRight">

      <div class="row">
        <div class="col-md-4">
          <div id="nestable-menu">
            <%= link_to "Create New List", new_todo_list_path, :class => "btn btn-white btn-sm" %>

          </div>
        </div>
      </div>
      <div class="row">
        <% @todo_lists.each do |todo_list| %>

        <div class="col-lg-4">
          <div class="ibox">
            <div class="ibox-content">
              <h3><%= link_to todo_list.title, todo_list %>
                -
                <span class="small"><%= todo_list.description %></span>
              </h3>
              <p class="small">
                <i class="fa fa-hand-o-up"></i>
                Drag task between list</p>

              <div class="input-group">

                <%= form_for([@todo_list, @todo_list.todo_items.build]) do |f| %>
                <%= f.text_field :content, placeholder: "New Todo" %>
                <%= f.submit %>
                <% end %>

              </div>

              <ul class="sortable-list connectList agile-list" id="todo">
                <%= todo_list.todo_items.each do |todo_items| %>
                <li class="warning-element" id="task1">

                  <%= todo_items.content %>

                  <div class="agile-detail">

                    <%= link_to "Delete", todo_list_todo_item_path(todo_list, todo_items.id), :class=>"pull-right btn btn-xs btn-danger", method: :delete, data: { confirm: "Are you sure?" } %>
                    <i class="fa fa-clock-o"></i>
                    <%= todo_items.created_at.strftime('%m/%d/%Y %I:%M %p') %>
                  </div>
                </li>
                <% end %>
              </ul>
            </div>
          </div>
        </div>
        <% end %>
      </div>
    </div>

todo_lists_controller:

        class TodoListsController < ApplicationController
      before_action :set_todo_list, only: [:show, :edit, :update, :destroy]

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

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

      end

      # GET /todo_lists/new
      def new
        @todo_list = TodoList.new
      end

      # GET /todo_lists/1/edit
      def edit
      end

      # POST /todo_lists
      # POST /todo_lists.json
      def create
        @todo_list = TodoList.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
        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.destroy
        respond_to do |format|
          format.html { redirect_to root_url, notice: 'Todo list was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

      private
        # 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, :description)
        end
    end

todo_items_controller:

                class TodoItemsController < ApplicationController
                before_action :set_todo_list
                before_action :set_todo_item, except: [:create]

                def create
                    @todo_item = @todo_list.todo_items.create(todo_item_params)
                    redirect_to @todo_list
                end



                def destroy
                    if @todo_item.destroy
                        flash[:success] = "Todo List item was deleted."
                    else
                        flash[:error] = "Todo List item could not be deleted."
                    end
                    redirect_to @todo_list
                end

                def complete
                    @todo_item.update_attribute(:completed_at, Time.now)
                    redirect_to @todo_list, notice: "Todo item completed"
                end

                private

                def set_todo_list
                    @todo_list = TodoList.find(params[:todo_list_id])
                end

                def set_todo_item
                    @todo_item = @todo_list.todo_items.find(params[:id])
                end

                def todo_item_params
                    params[:todo_item].permit(:content)
                end

            end

路线:

  resources :todo_lists do
    resources :todo_items do
      member do
          patch :complete
          end
    end
  end

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

我最终得到它的工作!我最不得不做的就是改变:

<%= form_for([@todo_list, @todo_list.todo_items.build]) do |f| %>

<%= form_for([todo_list, TodoItem.new],:remote => true) do |f| %>