【发布时间】:2025-11-29 21:20:02
【问题描述】:
我已经制作好了表格:
<tbody>
<% @player.bases.each do |basis| %>
<td><%= basis.id %></td>
<td><%= image_tag(basis.image_url(:thumb), class: 'thumbnail') %></td>
<td><%= link_to basis.name, basis %></td>
<td><%= basis.cost %></td>
<td><%= basis.short_info %></td>
<td>
<%= form_for @player, url: {:controller => 'players', :action => :remove_detail} do |f| %>
<%= f.hidden_field :type, :value => 'basis' %>
<%= f.hidden_field :detail_id, :value => basis.id %>
<%= f.submit 'Remove',class: 'btn btn-danger' %>
<% end %>
</td>
<% end %>
</tbody>
在我的路线中,我添加了这个:
resources :players do
collection do
get 'search'
post 'remove_detail'
end
end
我的players_controller.rb 中有remove_detail,我已将此操作添加到before_action 以获取当前玩家。但是,当我按下“删除”按钮时,它会引发错误并尝试运行控制器的 update 操作。为什么?
我的before_action:
before_action :set_player, only: [:show, :edit, :update, :destroy, :remove_detail]
我的remove_detail:
def remove_detail
type = params['type']
id = params['detail_id']
if type == 'basis'
basis = Basis.find(id)
name = basis.name
@player.bases.delete(basis)
end
redirect_to @player, notice: "#{name} detail is removed"
end
【问题讨论】:
-
您介意发布整个模板吗?如果提供的是局部的,您能否添加一个呈现该局部的模板?
标签: ruby-on-rails ruby forms routing actioncontroller