gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:55:51 25 4
gpt4 key购买 nike

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

更新

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

<%  @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我已经做了很多相当多的研究来寻找茧,多态路线上的导轨指南和其他几个 stackoverflow 链接。

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

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

更新:

我已经试过了 <%= 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 的待办事项列表。

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

>> 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: 这是我的待办事项列表 Controller 中发生错误的地方。没有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

Controller :

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 %>

最佳答案

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

发生的事情是,当您运行 @todo_list = current_user.todo_lists.new 时,@todo_lists 变量(首先是一个持久列表数组)发生了变化。该命令实际上将一个新的(未保留的)列表添加到 @todo_lists 数组的末尾(对我来说似乎是错误的行为,但以前发生在我身上),因此当您的 View 循环遍历它们时,最后一个没有id,无法为其创建路径。

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

从 Controller 中取出 @todo_list,在 View 中使用它的地方,改为执行 current_user.todo_lists.build。这应该实例化一个新列表而不改变@todo_lists变量。

关于ruby - Rails 4 嵌套表单 link_to 编辑不在循环中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25756078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com