Rails 4嵌套表单link_to编辑不在循环中工作

时间:2014-09-10 01:40:23

标签: ruby ruby-on-rails-4 crud nested-forms link-to

我在rails中有一个嵌套的路由/模型/表单。在我的索引页面上,我列出了下面有todo_items的todo_lists。我希望能够点击我的待办事项列表标题然后它将我带到编辑页面。我研究了多态路由和嵌套路由。

更新

这是我的修复,以阻止它创建虚拟待办事项列表。

<%  @current_todo_lists.each do |list| %>
    <% if list.id %>
        <div class="panel">
            <p><strong><%= link_to list.title ,edit_todo_list_path(list)%></strong></p>
            <% list.todo_items.each do |todo_item| %>
                <p><%= todo_item.description %></p>
            <% end %>
        </div>
    <% end %>
<% end %>

github link

Link_to rails nested form edit

polymorphic_path not generating correct path 我已经做了很多关于cocoon的研究,关于多态路由的rails指南和其他几个stackoverflow链接。

我没有成功完成任何这些工作。

这是索引页面,其中列出了所有带有todo_items的todo_lists。它通过一个循环来列出每个待办事项列表以及用它创建的相应项目

更新

我已经尝试了<%= link_to list.title, edit_todo_list_path(list) %><%= link_to list.title, edit_todo_list_path(@list) %>。 我得到的错误信息是:

ActionController::UrlGenerationError at /todo_lists No route matches {:action=>"edit", :controller=>"todo_lists", :id=>nil} missing required keys: [:id] 与@todo_list相同的配置会产生相同的错误。 基本上它无法找到带有id的Todo List。

在控制台确实给了我一个结果。所以我错过了一些东西。

>> t = TodoList.find(1)
=> #<TodoList id: 1, title: "First todo List with a modal", created_at: "2014-09-09 23:02:27", updated_at: "2014-09-09 23:02:27", user_id: 1>
>>

更新2:这是我的待办事项列表控制器中发生错误的地方。没有id就无法找到它。

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


<% @todo_lists.each do |list| %> 
    <p><strong><%= link_to list.title, edit_polymorphic_path(@todo_list) %></strong></p>
    <% list.todo_items.each do |todo_item| %>
        <p><%= todo_item.description %></p>
    <% end %>
<% end %>

到目前为止<p><strong><%= link_to list.title, edit_polymorphic_path(@todo_list) %参数已经是@todo_list(s),@ todo_lists(s),todo_items等等。

型号:

class TodoList < ActiveRecord::Base
    has_many :todo_items, dependent: :destroy
    accepts_nested_attributes_for :todo_items, allow_destroy: true
    validates_presence_of :title
end

class TodoItem < ActiveRecord::Base
    belongs_to :todo_list
end

控制器:

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
    #find current user todo lists/items
    @todo_lists = current_user.todo_lists
    @todo_items = current_user.todo_items
    #create a new user todo list
    @todo_list = current_user.todo_lists.new
    # builder for todo list _form
    3.times{ @todo_list.todo_items.build }
  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
    3.times{ @todo_list.todo_items.build }
  end

  # GET /todo_lists/1/edit
  def edit
    #@todo_list = TodoList.find(todo_list_params)
    @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_items_attributes: [:description, :_destroy])
    end
end



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

  # GET /todo_items
  # GET /todo_items.json
  def index
    @todo_items = TodoItem.all
  end

  # GET /todo_items/1
  # GET /todo_items/1.json
  def show
    @todo_item = TodoItem.find(params[:id])
  end

  # GET /todo_items/new
  def new
    @todo_item = @todo_list.todo_items.build
  end

  # GET /todo_items/1/edit
  def edit
    @todo_item = TodoItem.find(params[:id])
  end

  # POST /todo_items
  # POST /todo_items.json
  def create
    @todo_item = @todo_list.todo_items.build(todo_item_params)

    respond_to do |format|
      if @todo_item.save
        format.html { redirect_to [@todo_list,@todo_item], notice: 'Todo item was successfully created.' }
        format.json { render :show, status: :created, location: @todo_item }
      else
        format.html { render :new }
        format.json { render json: @todo_item.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /todo_items/1
  # DELETE /todo_items/1.json
  def destroy
    @todo_item = TodoItem.find(params[:id])
    @todo_item.destroy
    respond_to do |format|
      format.html { redirect_to todo_list_todo_items_url, notice: 'Todo item was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_todo_item
      @todo_item = TodoItem.find(params[:id])
    end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def todo_item_params
      params.require(:todo_item).permit(:description, :text, :todo_list_id)
    end
end

最后是表格。现在它允许添加一个todo_list和一些todo_items就像练习一样。我计划稍后使用一些Ajax来进行动态创建。并有一个不同的编辑形式。

<%= 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>
  <div>
    <%= f.fields_for :todo_items do |builder| %>
      <%= builder.label :description, "Items" %>
      <%= builder.text_field :description %>
      <%= builder.check_box '_destroy' %>
    <% end %>

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

2 个答案:

答案 0 :(得分:2)

您正在尝试创建一个尚未保留的资源的链接:

#create a new user todo list
@todo_list = current_user.todo_lists.new

由于您从未致电@todo_list.save,因此该记录没有ID且无法路由到。

我认为你要做的是:

<% @todo_lists.each do |list| %> 
  <p><strong><%= link_to list.title, edit_polymorphic_path(id: list.to_param) #not @todo_list! %></strong></p>
  <% list.todo_items.each do |todo_item| %>
    <p><%= todo_item.description %></p>
  <% end %>
<% end %>

我会认真考虑重命名@todo_list - &gt; @new_todo_list因为它非常混乱。


更新

“我甚至可以点击一个按钮打开一个模式,它将在同一页面上创建一个新的待办事项列表。”

您应该做的是让用户通过向/todo_list发布带有AJAX POST请求的表单来创建新资源,然后使用新列表(和编辑链接)更新页面。

您现在正在做的是创建一个新的todo_list,它只在rails完成处理请求之前存在于服务器内存中。

答案 1 :(得分:2)

我认为这个问题实际上有点棘手,因为edit_todo_list_path(list)似乎会抛出同样的错误。

当您运行@todo_lists时,@todo_list = current_user.todo_lists.new变量(第一个是持久列表数组)会发生变化。该命令实际上在@todo_lists数组的末尾添加了一个新的(未加载的)列表(对我来说似乎是错误的行为,但它之前发生在我身上),所以当你的视图循环遍历它们时,最后一个没有id,也无法为它创建路径。

解决方案是(我认为)在您使用@todo_lists变量后生成该变量。

从控制器中取出@todo_list,并在视图中使用它,而不是current_user.todo_lists.build。这应该实例化一个新列表,而不用更改@todo_lists变量。