在我的RoR项目中,我的删除方法无法工作。这很奇怪,因为它一天前就开始工作了,但现在它所做的一切都让我重定向到“朋友”页面。另外要注意的是,弹出的对话框“您确定吗?”也不会出现在删除朋友时,当它是以前的工作。我在网上读到了一些解决方案,声明将"//= require“和"//= require jquery_ujs”放在您的javascript文件中,但我只能在我的"app/assets/config“目录中找到我的manifest.js文件。
任何帮助都将不胜感激。
index.html.erb
<% if user_signed_in? %>
<table class="table table-striped table-bordered table-hover">
<thead class="thead-dark">
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Twitter</th>
<th>User ID</th>
<th></th>
</tr>
</thead>
<tbody>
<% @friends.each do |friend| %>
<% if friend.user == current_user %>
<tr>
<td>
<%= link_to friend.first_name + " " + friend.last_name, friend, style: 'text-decoration:none' %>
</td>
<td><%= friend.email %></td>
<td><%= friend.phone %></td>
<td><%= friend.twitter %></td>
<td><%= friend.user_id %></td>
<td>
<%= link_to 'delete',
friend,
:method => :delete,
:confirm => "are you sure?",
class: "btn btn-danger btn-sm" %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<br>
<% else %>
<h1>Welcome to the Friend App</h1>
<% end %>manifest.js
//= link_tree ../images
//= link_tree ../builds
//= require jquery
//= require jquery_ujs发布于 2021-12-31 05:59:11
在Rails 7中,指定delete方法的“旧”方法不起作用。我的猜测是从rails到turbo的转变是罪魁祸首。rails 7中的Rails-ujs在5.1版时被移动到Rails中和Hotwire Turbo取代了它。
我就是这样解决问题的:
路由: destroy_user_session删除/用户/注销(.:format)设计/会话#销毁
.html.erb:
<%= link_to t('navigation.sign_out'), destroy_user_session_path, method: :delete, class: "btn btn-danger ml-3" %>html:(注意数据-方法=“删除”)
<a class="btn btn-danger ml-3" rel="nofollow" data-method="delete" href="/users/sign_out"><span class="translation_missing" title="translation missing: en.navigation.sign_out">Sign Out</span></a>错误:没有路由匹配获得“/user/ route”
(溶液源)解决了的问题
.html.erb:
<%= link_to t('navigation.sign_out'), destroy_user_session_path, data: { "turbo-method": :delete }, class: "btn btn-danger ml-3" %>.html:(注意数据涡轮-方法=“删除”)
<a data-turbo-method="delete" class="btn btn-danger ml-3" href="/users/sign_out"><span class="translation_missing" title="translation missing: en.navigation.sign_out">Sign Out</span></a>发布于 2022-02-04 15:07:39
如果您想让确认消息开箱即用,只需为rails 7做一个简短的说明:
<%= link_to t('navigation.sign_out'),
destroy_user_session_path,
data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' },
class: "btn btn-danger ml-3" %>发布于 2022-10-10 08:03:45
Rails 7使用Turbo和刺激框架来提高前端性能。
你需要安装涡轮和刺激。在终端上运行以下命令:
$ rails importmap:install
$ rails turbo:install stimulus:install请确保您使用的turbo_method如下:
<%= link_to "Sign Out", destroy_user_session_path, data: { turbo_method: :delete }, class: "nav-link" %>https://stackoverflow.com/questions/70446101
复制相似问题