我正在使用Rails 4.2.3。在我的「應用程序/控制器/ user_objects_controller.rb」控制器的文件,我已經定義了這個方法...嘗試使Rails Ajax調用時出現ActionView :: Template :: Error錯誤
def find_totals
respond_to do |format|
@current_user = User.find(session["user_id"])
format.json {
@month_total = UserObject.find_total_by_user_object_month_and_year(session["user_id"], params[:object], params[:month], params[:year])
@year_total = UserObject.find_total_by_user_object_and_year(session["user_id"], params[:object], params[:year])
render :json => {:month => @monthTotal,
:year => @yearTotal }
}
end
end
我試圖使用Ajax調用這個方法,所以在我的「./app/assets/ javascripts/user_objects.js.coffee「腳本,我有
updateTotal = (arg) ->
object = $('#user_object_object').val()
date = $("#datepicker").datepicker('getDate')
day = $('#user_object_day').val()
month = date.getMonth() + 1
year = date.getFullYear()
if (object != "" && day != "")
# Populate the month and year totals
$.ajax
url: "/user_objects/find_totals"
type: 'GET'
data: {month: month, year: year, object: object}
success: (data) ->
alert(data);
$('#month_total').val(data)
$('#year_total').val(data)
error: ->
alert "Something went wrong getting month and year total"
但我總是得到錯誤條件。在我的Rails服務器中打印下面的日誌:
D, [2016-02-24T15:42:24.475092 #12929] DEBUG -- :
D, [2016-02-24T15:42:24.475206 #12929] DEBUG -- :
D, [2016-02-24T15:42:24.475236 #12929] DEBUG -- :
D, [2016-02-24T15:42:24.475265 #12929] DEBUG -- :
I, [2016-02-24T15:42:24.475505 #12929] INFO -- : Started GET "/user_objects/find_totals?month=2&year=2016&object=3" for 127.0.0.1 at 2016-02-24 15:42:24 -0600
I, [2016-02-24T15:42:24.475539 #12929] INFO -- : Started GET "/user_objects/find_totals?month=2&year=2016&object=3" for 127.0.0.1 at 2016-02-24 15:42:24 -0600
F, [2016-02-24T15:42:24.500710 #12929] FATAL -- :
ActionView::Template::Error (undefined method `object' for nil:NilClass):
2:
3: <p>
4: <strong>object:</strong>
5: <%= @user_object.object %>
6: </p>
7:
8: <p>
app/views/user_objects/show.html.erb:5:in `_app_views_user_objects_show_html_erb__2677150194333288712_70363943163180'
F, [2016-02-24T15:42:24.500751 #12929] FATAL -- :
ActionView::Template::Error (undefined method `object' for nil:NilClass):
2:
3: <p>
4: <strong>object:</strong>
5: <%= @user_object.object %>
6: </p>
7:
8: <p>
app/views/user_objects/show.html.erb:5:in `_app_views_user_objects_show_html_erb__2677150194333288712_70363943163180'
任何人都知道這個錯誤的含義以及我如何解決它?
是的,在我的routes.rb中添加一個「get:find_totals,on::collection」資源:user_objects do「部分解決了它。 – Dave