Button_to在电子邮件中不发布 [英] Button_to in email not posting

查看:203
本文介绍了Button_to在电子邮件中不发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看此问题的演变的更新



在我的网站上,每个用户都有一个仪表板,他/他可以点击链接ACCEPT或DECLINE请求。根据所点击的内容,请求记录将与相关状态进行PATCH。为了使用户更容易,我试图将这个仪表板嵌入到他们的电子邮件中,以免他们直接去网站;想到一个看起来像这样的电子邮件:



您好,



您有以下请求,请点击请求旁边的ACCEPT / DECLINE




  • 请求A :ACCEPT,DECLINE

  • 请求B:ACCEPT,DECLINE



....



到目前为止这项工作的唯一方法是为电子邮件中使用的链接设置一组并行的GET路由,相对于实际网站仪表板中使用的链接的PATCH路由。



想知道是否有更好的方法?



路线

 补丁'库存/:id / accept',到:'库存#接受',如:'lender_accept'
patch'库存/:id / ',to:'库存#减少',如:'lender_decline'
获得'库存/:id / accept_email'到:'库存#接受',如:'lender_accept_email'
获取库存/ :id / decline_email',to:'inventory#den',as:'lender_decline_email'

电子邮件

 <%=#{link_to'ACCEPT',lender_accept_email_url(借),方法::patch}或# link_to'DECLINE',lender_decline_email_url(borrow)}%> 

在网站信息中心链接

 <%=#{link_to'ACCEPT',lender_accept_path(借用),方法::patch}或#{link_to'DECLINE',lender_decline_path(借用),方法::patch}%> ; 






更新 p>

Ok尝试使用 button_to 生成一个表单POST以避免使用GET做POST,patchy解决方案以上,仍然没有工作...



路线

  c> post'inventory / / id / accept',to:'inventory#accept',as:'lender_accept'
post'inventory / ,如:'lender_decline'

邮件视图:

 <%=#{button_to'YES',lender_accept_url(借用),方法::post,id:accept#{borrow.id },style:background-color:green;颜色:白色; width:40px;显示:inline}%> 
<%=#{button_to'NO',lender_decline_url(借),方法::post,id:decline#{borrow.id},style:background -color:gray; width:40px; display:inline}%>

检查电子邮件中的元素,以确认 button_to 正在生成适当的代码:

 < form action =inventory / 2037 / declinemethod =posttarget =_ blankonsubmit =return window.confirm(& quot;您正在向外部页面提交信息。 \\ n你确定吗?& quot;);>< div>< input style =background-color:gray; width:40px; display:inlinetype =submitvalue =NO> < / div>< / form> 
< div>
< input style =background-color:gray; width:40px; display:inlinetype =submitvalue = NO>
< / div>
< / form>

在我的邮件设置中,我将主机正确设置为我的域名,所以当我得到电子邮件并点击按钮,我被适当地拿到 / inventory / 2037 / decline ,但我仍然收到错误,因为显然日志说我还在尝试去为GET ...为什么?

  2014-08-17T06:18:03.206205 + 00:00 app [ web.1]:ActionController :: RoutingError(No route matches [GET]/ inventory / 2037 / decline):


想知道有没有更好的方法?


使 GET 请求是理想的解决方案。不要使用任何其他请求。它不受电子邮件客户端的支持,并可能增加大的安全漏洞。



您的误解是,大型网站使用 post / patch 请求在电子邮件中执行此类操作。他们都使用获取请求,它们包括一些令牌,当用户访问该网站时,该令牌被绑定/触发该特定操作。


确定按钮_to生成一个表单POST以避免使用GET做POST,就像上面的patchy解决方案,仍然没有工作。


显然,这不行。没有 rails JavaScript文件。而且,即使包含它也不会奏效,因为 csrf-token 令牌。 rails应用程序默认情况下不允许外部提交表单。



我的建议是将用户发送到您的仪表板,其中包含一些额外的参数,如www。 example.com/dashboard?take_action=accept 并使用JavaScript触发所需的操作。这将是最佳做法。这将避免您使用 lender_accept 请求作为获取



为了更精确,不要这样做

 获取'库存/:id / accept_email'到:'库存#接受',为:'lender_accept_email'
获得库存/:id / reject_email到:库存#拒绝,如:'lender_decline_email'

这是坏的由于获取请求不适用于更新/删除



由于您想将操作链接到您的电子邮件中,您应该将仪表板链接一些额外的参数。现在,当您看到传递给您的电子邮件的参数时,JavaScript将执行 accept / decline 操作。



更多:




SEE UPDATE for evolution of this question

On my website, each user has a dashboard where s/he can click a link to either ACCEPT or DECLINE an request. Depending on what is clicked, the Request record is then PATCHed with the relevant status. To make it easier for users, I'm trying to embed this dashboard in an email to them so that they never have to go to the website directly; think of an email that looks like this:

Hi there,

You have the following requests, click ACCEPT/DECLINE next to the request to do so

  • Request A: ACCEPT, DECLINE
  • Request B: ACCEPT, DECLINE

....

The only way to make this work thus far has been to have a parallel set of GET routes for the links used in email, versus the PATCH routes for the links used in the actual website dashboard.

Wondering if there's a better way of doing this?

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 in email

<%= "#{link_to 'ACCEPT', lender_accept_email_url(borrow), method: :patch} or #{link_to 'DECLINE', lender_decline_email_url(borrow)}" %>

Link on website dashboard

<%= "#{link_to 'ACCEPT', lender_accept_path(borrow), method: :patch} or #{link_to 'DECLINE', lender_decline_path(borrow), method: :patch}" %>


UPDATE

Ok tried the button_to to generate a form to POST to avoid using GET to do POST as was the "patchy" solution above, still not working...

Routes:

post 'inventories/:id/accept', to: 'inventories#accept', as: 'lender_accept'
post 'inventories/:id/decline', to: 'inventories#decline', as: 'lender_decline'

Mailer view:

<%="#{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"}" %>

I did an inspect element on the email as well just to confirm that the button_to was generating the appropriate code:

<form action="inventories/2037/decline" method="post" target="_blank" onsubmit="return window.confirm(&quot;You are submitting information to an external page.\nAre you sure?&quot;);"><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>

In my mailer settings, I set the host properly to my domain name, so when I get the email and click the button, I get taken to /inventories/2037/decline appropriately, but I still get the error, because apparently the logs say I'm still trying to go for GET... why is that??

2014-08-17T06:18:03.206205+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/inventories/2037/decline"):

解决方案

Wondering if there's a better way of doing this?

Making GET request is ideal solution. Don't use any other request. It either be not supported by email client and potentially add big security hole.

It is your misconception that big websites use post/patch requests to perform such action in email. They all use get requests, they include some token which is tied up/trigger that particular action when user visits there website.

OK tried the button_to to generate a form to POST to avoid using GET to do POST as was the "patchy" solution above, still not working.

Obviously, it won't work. There is no rails JavaScript file. Moreover, even if you include it it won't work either, because of csrf-token token. External form submission is not allowed by default in rails application.

My suggestion is to send user to your dashboard with some extra parameters like www.example.com/dashboard?take_action=accept and use JavaScript to trigger the required action. This will is best practice. This will avoid you using lender_accept request as get.

To to more precise, don't do this.

get 'inventories/:id/accept_email', to: 'inventories#accept', as: 'lender_accept_email'
get 'inventories/:id/decline_email', to: 'inventories#decline', as: 'lender_decline_email'

This is bad. Since, get request isn't for updating/deleting.

Since, you want to link this actions into your email, you should put dashboard link with some extra parameter. Now, with JavaScript perform accept/decline action whenever you see those parameters which you passed to your email.

More:

这篇关于Button_to在电子邮件中不发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆