ruby-on-rails - Rails 新的初始化对象创建一个空记录
问题描述
我有两个模型叫做TodoList
and TodoItem
。在 TodoItem 索引页面中,我正在显示新表单和待办事项列表。一切正常但它在浏览器中生成一个空记录。
class TodoItem < ApplicationRecord
belongs_to :todo_list
end
class TodoList < ApplicationRecord
has_many :todo_items, dependent: :destroy
end
控制器有:
class TodoItemsController < ApplicationController
def index
@todo_list = TodoList.find(params[:todo_list_id])
@todo_items = @todo_list.todo_items
@new_todo = @todo_list.todo_items.new
end
def create
@todo_list = TodoList.find(params[:todo_list_id])
@todo_item = @todo_list.todo_items.new(params.require(:todo_item).permit(:description, :complete_at))
if @todo_item.save
redirect_to todo_list_todo_items_path(@todo_list)
end
end
end
index.html.erb
<div>
<div>
<% form_with(model: [@todo_list, @todo_item], local: true) do |f| %>
<% f.text_field :description %>
<% f.submit %>
<% end %>
</div>
<ul>
<% @todo_items.each do |todo_item| %>
<li><%= todo_item.description %></li>
<% end %>
</ul>
</div>
解决方案
class TodoItemsController < ApplicationController
# use callbacks instead of repeating yourself
before_action :set_todolist, only: [:new, :create, :index]
def index
@todo_items = @todo_list.todo_items
@todo_item = TodoItem.new
end
def create
@todo_item = @todo_list.todo_items.new(todo_list_params)
if @todo_item.save
redirect_to [@todo_list, :todo_items]
else
render :new
end
end
private
def set_todolist
@todo_list = TodoList.find(params[:todo_list_id])
end
# use a private method for your params whitelist for readibility
# it also lets you reuse it for the update action
def todo_list_params
params.require(:todo_item)
.permit(:description, :complete_at)
end
end
您在索引操作中设置不同的实例变量 ( @new_todo
)。[@todo_list, @todo_item]
多态路由助手从数组上的调用中查找路由助手。因此,如果@todo_item
是 nil,它会改为调用todo_lists_path
- 哎呀!
您还需要考虑如何响应无效数据。通常在 Rails 中,这意味着渲染新视图。如果您在另一个视图(例如索引视图)中呈现表单,则重新呈现相同的视图可能会有些棘手,因为您必须将所有相同的数据设置为导致重复的操作。
推荐阅读
- css - 我是否正确使用了 flex-box 并且是最优化的方式?
- react-native - Android 中 React Native 的 ImageEditor.cropImage 问题
- javascript - javascript中的命名空间标准
- javascript - 如何将文件上传限制为仅 .xlxs 和 .docx 文件
- r - install.packages 省略了源文件
- google-sheets - 将延迟从 20 分钟更改为 1 分钟
- r - 基于具有指定格式(例如,删除线)的单元格消除 R 中的 Excel 行?
- java - 缺少 Idlj 编译器 (Linux/JDK 11)
- excel - VBA:过滤和排序预先存在的自动过滤器范围
- ruby - Ruby Gems 开发中的分享