簡體   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