无法通过电子邮件向我的api发送更新操作

时间:2016-10-21 11:10:21

标签: ruby-on-rails api email routes actionmailer

我有一个rails 5 app(API模式)。我正在引导一个标记评论功能。我在注释表中添加了一个标记属性和一个禁用属性(因为我不想完全销毁注释,只是在禁用时将它们隐藏在前面)。

当评论被标记时,我会通过评论,一些关于它的元数据以及将禁用属性更新为true的链接向我自己发送一封电子邮件(通过ActionMailer)。

我的问题是,当我在邮件上点击此链接时,它不会更新属性。

以下是明确的代码:

routes.rb中:

resources :comments, only: [:create, :update] do
   member do
     post 'flag'
   end
end

控制器/ API / V1 / comments_controller.rb:

def update
    @comment = Comment.find(params[:id])
    new_status = @comment.disabled ? false : true

    # binding.pry => I'm not even reaching that method

    if @comment.update(disabled: new_status)
       CommentDisabledMailer.send_comment_disabled_confirmation(@comment).deliver_now
       redirect_to "https://media.giphy.com/media/YfGkjrnVIk3jq/giphy.gif" # a funny gif url
    else
       render json: { errors: @comment.errors.messages }, status: :unprocessable_entity
   end
end

def flag
   @comment.update(flagged: @comment.flagged + 1)
   render json: @comment, serializer: Api::V1::CommentSerializer, status: 201, root: nil
   FlagMailer.send_flag_mail(@comment).deliver_now
end

视图/ flag_mailer / send_flag_mail.html.erb:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <!-- some meta data about my comment -->

    <h2> <%= link_to "Disabled this comment", api_v1_comment_url(@comment), method: :put, action: :update %> </h2>

  </body>
</html>

我通过邮递员标记评论时收到邮件,标记的属性已更新,但当我点击邮件上的“禁用此评论”时,它会打开一个带有rails错误的新窗口: No route matches [GET] "/api/v1/comments/5",注释的disabled属性未更新,我显然没有收到应确认注释已被禁用的邮件(参见控制器中的update方法)。

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

你可以试试这个

<a href="<%= api_v1_comment_url(@comment) %>" data-method="put">Disabled this comment</a>