views:

16

answers:

1

Greetings,

I have an application which contains a calendar as alternative index view for Courses (Project has_many Courses). There I have two arrows for navigating to the next/previous month, it works via AJAX. Now, that's my action for updating the calendar:

def update_calendar
  @project = Project.find params[:id]
  @date = Date.parse(params[:date]).beginning_of_month
  @courses = @project.courses.all(:conditions => {:date => (@date - 1.month)..(@date + 1.month)})

  respond_to do |format|
    # format.html { redirect_to :action => 'index' }
    format.js { render :partial => 'calendar', :locals => {:calendar_date => @date, :courses => @courses} }
  end
end

The important part is format.js { ... }. I thought it should just answer js/AJAX requests, but it doesn't. It renders the partial when I hit the URL http://localhost:3000/projects/1/update_calendar?date=2010-08-01 for example. I don't want that behaviour, I just want it to answer correctly when it's coming in via AJAX. Do I need to use request.xhr?? And what is format.js { ... } supposed to do than?

Best regards

Tobias

A: 

You need to tell it what you want to happen when someone goes to that action with an html request, ie you need to put your format.html block back in. If you don't have the braces it will do the default behaviour which is to show the update_calendar view. You probably want to redirect to a different action though.

Max Williams
Ah, I see. Though redirecting might not be a good idea, since that page will then be inserted in the calendar div. But good thing, I can render a 404 or another error/exception. Thanks a lot!
Tobias