Quote

Ruby On rails: Problem related to routingy

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

var formData = $('.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

The solution is

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s