我在 trackers_controller.show 的 show erb 中有一个“删除”按钮:
<%= form_for :user_tracker, :url => user_tracker_path,:method => :delete do |f| %>
<%= f.hidden_field :tracker_id, :value => @tracker.id %>
<%= f.submit :save, :value => 'Remove' %>
<% end %>
这工作正常并调用 user_trackers_controller.destroy
用户模型如下所示:
has_many :user_trackers
has_many :trackers, :through => :user_trackers
如果我将完全相同的按钮放在另一个 erb 中,我会收到此错误:
No route matches {:action=>"show", :controller=>"user_trackers"}
我有一些不同的组合,例如 :html => {:method => :delete }
如果我这样尝试
<% current_user.user_trackers.each do |user_tracker| %>
<%= user_tracker.tracker %>
<%= form_for user_tracker, :method => :delete do |f| %>
<%= f.submit :delete, :value => 'Remove' %>
<% end %>
我得到相同的路由错误
以下是路线:
user_trackers GET /user_trackers(.:format) user_trackers#index
POST /user_trackers(.:format) user_trackers#create
new_user_tracker GET /user_trackers/new(.:format) user_trackers#new
edit_user_tracker GET /user_trackers/:id/edit(.:format) user_trackers#edit
user_tracker GET /user_trackers/:id(.:format) user_trackers#show
PUT /user_trackers/:id(.:format) user_trackers#update
DELETE /user_trackers/:id(.:format) user_trackers#destroy
我不明白,为什么在无关的erb中不会发现这是破坏?
编辑: 这似乎可行,但 html 会生成 id 和类,如 edit_user_tracker_7 以及 javascript 以使其成为删除,所以看来我仍然有问题:
<%= form_for user_tracker, :url => user_tracker_path(user_tracker), :method => :delete do |f| %>
<%= f.hidden_field :tracker_id, :value => user_tracker.tracker_id %>
<%= f.submit :delete, :value => 'Remove from my portfolio' %>
<% end %>