如何解决在每个循环中渲染一个 simple_form,rails 6
我正在创建一个购物清单应用,每个用户都有一个清单,每个清单都有很多项目。
在显示页面上,我遍历用户列表并显示列表中的每个项目,并打印出项目名称、描述和更新项目的链接。
问题是,更新表单总是使用每次迭代中的第一个项目而不是其分配的项目。
我可以有一个包含 5 行项目的列表,并且每个项目的更新链接将始终填充该行中的第一个项目。
我是 Rails 的新手,因此我们将不胜感激!
路线
Rails.application.routes.draw do
patch 'items/pick/:id',to: "items#pick",as: :pick
devise_for :users
root to: 'lists#index'
resources :lists do
resources :items
end
end
列表控制器
class ListsController < ApplicationController
def index
@lists = List.all
end
def show
@list = List.find(params[:id])
end
def new
@list = List.new
end
end
项目控制器
class ItemsController < ApplicationController
def new
end
def create
@list = List.find(params[:list])
@item = Item.new(item_params)
@item.list_id = @list.id
@item.user_id = current_user.id
if @item.save!
redirect_to list_path(@list),notice: "Item added to list."
else
render :new,notice: "Something went wrong..."
end
end
def edit
end
def update
@item = Item.find(params[:id])
if @item.update(venue_params)
redirect_to venue_path(@venue)
else
render :edit
end
end
def destroy
@item = Item.find(params[:id])
@item.destroy
redirect_to list_path(params[:list_id])
end
def pick
item = Item.find(params[:id])
if item.buyer.nil?
item.buyer = current_user.id
else
item.buyer = nil
end
item.save!
redirect_to lists_path
end
def item_params
params.require(:item).permit(:item_name,:description,:link,:price)
end
end
<div class="container">
<div class="row">
<div class="col-md-12 mt-5">
<h2>Hello <%= current_user.first_name %> ?</h2>
<p>Welcome to Listy,the new home of your gift lists</p>
<h4 class="mb-3">Manage: <%= link_to "Family lists",lists_path %></h4>
<div class="card p-3 mb-3">
<p><%= @list.title %> ID: <%= @list.id %> </p>
<% @list.items.sort.each do |item| %>
<div class="list-flex">
<div class="item_name"><%= item.item_name %></div>
<div class="description-show-page"><%= item.description %></div>
<div class="link"><%= render "lists/partial/update_item",list: @list,item: item %></div>
</div>
<% end %>
</div>
<%= render "lists/partial/add_item" %>
</div>
</div>
</div>
更新部分
<a type="" class="gray" data-toggle="modal" data-target="#updateModal">
<i class="far fa-edit"></i>
</a>
<!-- Modal -->
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Update item</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<%= simple_form_for [@list,item] do |f| %>
<h2> <%= item.id %></h2>
<%= hidden_field_tag 'list',@list.id %>
<%= f.input :item_name %>
<%= f.input :description %>
<%= f.input :link %>
<%= f.input :price %>
<%= f.submit "Add Item",class: "btn btn-outline-primary" %>
<% end %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-warning" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
每个更新链接都使用列表中的第一项呈现表单,90 是列表 ID
解决方法
接缝一个经典的编辑模态场景
当你加载页面时,你只会渲染一个模态。
您有 3 条记录,这意味着您需要 3 个模态
OR 您可以动态创建一个新的模态,例如 https://junit.org/junit4/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。