Shiva Bhusal
Shiva's Blog

Follow

Shiva's Blog

Follow

Ruby On rails: Problem related to routingy

Shiva Bhusal's photo
Shiva Bhusal
·Oct 9, 2014·

2 min read

Play this article

Problem


#

I have to edit a proposal among many proposals. When we click edit button a popup appears with all fields filled. There is a button ‘Verify’ to preview the proposal before sending to client via Email.

Suppose URL to edit proposal is proposals/:id/edit  It shows a popup with all fields filled and a ‘Send’ and ‘Verify’ button. URL for verify is proposals/:id/verify-proposal. When user edits and sends data is saved and an email is sent and the same form is rendered back filled to user with success message. Now when I click the verify button, routing error encountered, the application invokes update action throwing error.

ActiveRecord::RecordNotFound (Couldn't find Proposal with 'id'=verify-proposal): app/controllers/proposals\_controller.rb:66:in update'

Reason:: now URL where AJAX request is being sent is proposals/verify-proposal

Behind Scene: In routes.rb i have routed two URLs to same actions within which http method is checked and processed accordingly. URLs proposals/:id/verify-proposal (To verify edit of Individual Proposal) and proposals/verify-proposal (To verify newly to be created Proposal) are routed to verify_proposal action. To verify/preview I have used AJAX method to send data as

varformData= $('.form').serializeArray();
$.ajax({
url: 'verify-proposal',
type: 'post',
data:formData,
success: function (html) {
//Open Popup window then verify HTML comes from server ... ... ... } });
# routes.rb 
resources :proposals do
  collection do 
    post :verify_proposal, path: 'verify-proposal'
  end
  member do
    patch :verify_proposal, path: 'verify-proposal'
  end
end

The solution is

varformData= $('.form').serializeArray();
$.ajax({
url: '\bids\' + idOfBid + '\verify-proposal',
type: 'post',
data:formData,
success: function (html) {
//Open Popup window then verify HTML comes from server ... ... ... } });
 
Share this