可通过“正常”访问的 AJAXy 操作要求
您好,
我有一个应用程序,其中包含日历作为课程的替代索引视图(项目 has_many 课程)。我有两个箭头用于导航到下个月/上个月,它通过 AJAX 工作。现在,这就是我更新日历的操作:
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
重要的部分是 format.js { ... }
。我认为它应该只回答 js/AJAX 请求,但事实并非如此。当我点击 URL http://localhost:3000 时,它会呈现部分内容例如 /projects/1/update_calendar?date=2010-08-01。我不想要这种行为,我只是希望它在通过 AJAX 传入时能够正确回答。我需要使用 request.xhr 吗?
? format.js { ... }
应该做什么?
最好的问候
托比亚斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要告诉它当有人通过 html 请求执行该操作时你想要发生什么,即你需要将 format.html 块放回原位。如果你没有大括号,它将执行默认行为,即显示 update_calendar 视图。不过,您可能想重定向到不同的操作。
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.