繁体   English   中英

在controller#show Rails上发送发布请求

[英]Send post request on controller#show Rails

在我的应用程序中,用户可以选择拒绝请求。 我想给拒绝该请求的用户一个选项,以说明原因,以便其他用户有更好的主意。

在我的显示页面上,我有一个按钮可以打开引导模型

<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Deny
    </button>

    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Deny Request</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <%= form_for :pdform, url: deny_pdform_path, method: :put do |f| %>
              <%= f.text_area :reason, placeholder: 'Why are you denying this request?', class: 'form-control' %>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              <%= f.submit 'Deny', class: 'btn btn-danger' %>
            <% end %>
          </div>
        </div>
      </div>
    </div>

在此模型中,有一种表单可以更新pdforms拒绝reason

对于拒绝按钮,我有此操作

def deny
  if @pdform.save
    if current_user.principal?
      @pdform.principal_action = 'denied'
      redirect_to root_path
    elsif current_user.super?
      @pdform.super_action = 'denied'
      redirect_to root_path
    end
  end
end

为什么不将reasondenied状态一起放入数据库? 我的许可参数中有:reason ,所以这不是问题。 以模态提交表单时没有错误-原因就是没有通过。 有什么原因吗?

我认为您实际上并没有在deny操作中使用从表单传递的reason

另外,在分配principal_actionsuper_action之后,您似乎并未调用save

请尝试以下操作:

def deny
  @pdform.assign_attributes(pdform_params) # or whatever you've called the permitted params

  if current_user.principal?
    @pdform.principal_action = 'denied'
  elsif current_user.super?
    @pdform.super_action = 'denied'
  end

  if @pdform.valid?
    @pdform.save
    redirect_to root_path
  else
    # handle errors from an invalid @pdform
  end
end

看看是否可行,并让我知道您的反馈! 希望能帮助到你 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM