请参阅此问题的演变的更新
在我的网站上,每个用户都有一个仪表板,他/她可以在其中单击链接来接受或拒绝请求。根据单击的内容,请求记录将使用相关状态进行修补。为了让用户更方便,我尝试将此仪表板嵌入到发给他们的电子邮件中,这样他们就不必直接访问该网站;想象一封如下所示的电子邮件:
你好呀,
您有以下请求,请单击请求旁边的“接受/拒绝”来执行此操作
....
迄今为止,实现此目的的唯一方法是为电子邮件中使用的链接设置一组并行的 GET 路由,而不是为实际网站仪表板中使用的链接设置 PATCH 路由。
想知道是否有更好的方法来做到这一点?
Routes
patch 'inventories/:id/accept', to: 'inventories#accept', as: 'lender_accept'
patch 'inventories/:id/decline', to: 'inventories#decline', as: 'lender_decline'
get 'inventories/:id/accept_email', to: 'inventories#accept', as: 'lender_accept_email'
get 'inventories/:id/decline_email', to: 'inventories#decline', as: 'lender_decline_email'
电子邮件中的链接
<%= "#{link_to 'ACCEPT', lender_accept_email_url(borrow), method: :patch} or #{link_to 'DECLINE', lender_decline_email_url(borrow)}" %>
网站仪表板上的链接
<%= "#{link_to 'ACCEPT', lender_accept_path(borrow), method: :patch} or #{link_to 'DECLINE', lender_decline_path(borrow), method: :patch}" %>
UPDATE
好的尝试了button_to
生成一个 POST 表单以避免使用 GET 来执行 POST,就像上面的“不完整”解决方案一样,仍然不起作用...
Routes:
post 'inventories/:id/accept', to: 'inventories#accept', as: 'lender_accept'
post 'inventories/:id/decline', to: 'inventories#decline', as: 'lender_decline'
邮件视图:
<%="#{button_to 'YES', lender_accept_url(borrow), method: :post, id: "accept #{borrow.id}", style: "background-color:green; color: white; width: 40px; display: inline"} %>
<%="#{button_to 'NO', lender_decline_url(borrow), method: :post, id: "decline #{borrow.id}", style: "background-color:gray; width: 40px; display: inline"}" %>
我还对电子邮件进行了检查元素,只是为了确认button_to
正在生成适当的代码:
<form action="inventories/2037/decline" method="post" target="_blank" onsubmit="return window.confirm("You are submitting information to an external page.\nAre you sure?");"><div><input style="background-color:gray;width:40px;display:inline" type="submit" value="NO"></div></form>
<div>
<input style="background-color:gray;width:40px;display:inline" type="submit" value="NO">
</div>
</form>
在我的邮件程序设置中,我将主机正确设置为我的域名,因此当我收到电子邮件并单击按钮时,我会被带到/inventories/2037/decline
适当地,但我仍然收到错误,因为显然日志说我仍在尝试获取...为什么会这样?
2014-08-17T06:18:03.206205+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/inventories/2037/decline"):