简体   繁体   中英

Send post request on controller#show Rails

In my application a user has the option to deny a request. I want to give that user denying the request the option to say why so the other user has a better idea.

On my show page, I have a button that opens a bootstrap model

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

In this model there is a form to update the pdforms reason as to why it was denied.

For the deny button I have this action

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

Why would the reason not be getting put into the database along with the status of denied ? I have :reason in my permit params so this is not an issue. There is no error upon submitting the form in the modal - the reason is just not getting passed. Any reason for this?

I don't think you're actually using the reason passed from the form within the deny action.

Also, you don't seem to be calling save after assigning the principal_action or the super_action .

Try the following:

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

See if that works and let me know your feedback! Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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