Shalafister's
Shalafister's

Reputation: 881

Route does not match in jquery with custom action

I have custom controller actions called #load and #save called schedules_controller. I also have Boat Model, where Boat has_many: schedules and Schedule belongs_to :boat.

Here is the routes.rb;

resources :boats, except: :destroy do
      post '/schedules/load' => 'schedules#load'
      post '/schedules/save' => 'schedules#save'
  end

When I use this routes rake gives;

boat_schedules_load POST   /boats/:boat_id/schedules/load(.:format)    schedules#load
boat_schedules_save POST   /boats/:boat_id/schedules/save(.:format)    schedules#save

Then in my Schedules_controller;

class SchedulesController < ApplicationController
before_filter :load_parent

def save 
    h = params[:event] 
    h.each do |key, value|
        @boat.schedules.create date: value["date"], notes: value["notes"],  price: value["price"], promo: value["promo"], status: value["status"]
    end
end

def load

end


def load_parent
     @boat = Boat.find(params[:boat_id])
end
end

I have a template, where I show the schedule(calendar) and have Jquery, probably the Jquery path is wrong but I do not know how to use id in the path;

overwiev.html.erb;

......
<div id="backend"></div> <!--This is for the calendar-->


<script>
$(document).ready(function() {
    $('#backend').DOPBackendBookingCalendarPRO({

        'DataURL': '/schedules/load', #THIS IS WRONG I SHOULD SMT LIKE PUT /boat_id/schedules/load
        'SaveURL': '/:boat_id/schedules/save'
    });
});
</script>

So when I run the code, it gives an error of;

Routing Error

No route matches [POST] "/schedules/load"

I also have load.json.erb file where I would like to load data from database,

<% schedule = @boat.schedules.all %>

<% if schedule.blank? %>

{"2010-01-08": {"available":"1",
                           "bind":0,
                           "info":"",
                           "notes":"",
                           "price":"20",
                           "promo":"",
                           "status":""
  }}

<% else %>

{"<%= schedule.last.date %>": {"available":"<%= schedule.last.available %>",
                           "bind":"<%= schedule.last.bind %>",
                           "info":"<%= schedule.last.info %>",
                           "notes":"<%= schedule.last.notes %>",
                           "price":"<%= schedule.last.price %>",
                           "promo":"<%= schedule.last.promo %>",
                           "status":"<%= schedule.last.status %>"
  }}


<% end %>

Because I get boat id at the controller I think <% schedule = @boat.schedules.all %> code should works. However, I tried taking out the routes (not nested) as;

resources :boats
post '/schedules/load' => 'schedules#load'
post '/schedules/save' => 'schedules#save'

Then it gives an error;

ActiveRecord::RecordNotFound in SchedulesController#load

Couldn't find Boat with 'id'=

Upvotes: 1

Views: 103

Answers (1)

Gaurav Gupta
Gaurav Gupta

Reputation: 475

Comment the filter in your controller:

#before_filter :load_parent

if you want to use plain routes

Or you need to change path in your script code like:

$(document).ready(function() {
 $('#backend').DOPBackendBookingCalendarPRO({

    'DataURL': '/boats/' + boat_id + '/schedules/load', #THIS IS WRONG I SHOULD SMT LIKE PUT /boat_id/schedules/load
    'SaveURL': '/boats/' + boat_id + '/schedules/load'
 });
});
</script>

And need to pass boat_id

Upvotes: 1

Related Questions