저는 레일스와 아약스가 처음이에요. 그래서 간단한 앱을 만들면서 실험하고 있습니다. 나는 User와 Tasks (사용자는 많은 작업을 가지고있다)로 앱을 간단하게 만들었다. Devise를 사용하여 인증을 처리합니다. 나는 CRUD 작업을 설정하고 best_in_place (장소 편집 보석에서)를 구현하고 작동하도록했습니다. 다음으로 수행하고자하는 작업은 ajax를 추가하고 creating a 100% ajax CRUD과 같은 예제 리소스를 따라 삭제하는 것입니다. tasks_controller에서 생성 및 삭제에 필요한 모든 코드를 설정 한 후 I'm getting DELETE http://localhost:3000/tasks/26 500 (Internal Server Error)
을 방화 광 콘솔 로그에 표시합니다. 방화 광에서 콘솔에 인쇄 된 오류는 다음과 같습니다. 파괴 링크를 클릭하고 만들기와 동일하게 클릭하면 오류가 발생합니다 :레일즈 3 아약스가 작동하지 않습니다.
DELETE http://localhost:3000/tasks/26 500 (Internal Server Error)
jQuery.ajaxTransport.send
jQuery.extend.ajax
$.rails.rails.ajax
$.rails.rails.handleRemote
(anonymous function)
jQuery.event.dispatch
jQuery.event.add.elemData.handle.eventHandle
작업은 새로 고침 후 삭제되고 생성되었지만 AJAX는 작동하지 않습니다. 내가 잘못한 것을 알아 내려고 노력했지만 시간이 좀 걸리지 만 운이 아직 없다. 그래서 나는 계속 파고 들어갈 동안 여기서 도움을 얻을 수 있기를 바라고있다. 아래 내 설정은 다음과 같습니다
먼저 컨트롤러 코드 :
def destroy
@task = current_user.tasks.find(params[:id])
@task.destroy
flash[:notice] = "Successfully destroyed post."
respond_to do |format|
format.html { }
format.js { }
end
end
def create
@task = current_user.tasks.build(params[:task])
respond_with(@task) do |format|
if @task.save
flash[:notice] = "Task was created successfully!"
respond_to do |format|
format.html { redirect_to tasks_url }
format.xml { render :xml => @task }
format.json { render :json => @task}
format.js
end
else
format.html { render :action => :new }
format.js
end
end
end
작업 목록을 표시하고 (다른 뷰 물건없이)
<table>
<% @tasks.each do |task| %>
<tr>
<td><%= best_in_place task, :detail %></td>
<!-- <p><%= best_in_place task, :completed, type: :checkbox %></p> -->
<td><%= link_to "Destroy", task, :confirm => 'Are you sure?', :method => :delete, :remote => true, :id=>'delete' %></td>
</tr>
<% end %>
</table>
작성 양식을 삭제하는 부분 작업 목록 :
사실) do | f | %><퍼센트 = f.label : 상세 %>
<% = f.text_field % 이것은
이다> 상세 %> <퍼센트 = f.submit %의> <% 단부 application.html.erb에 대한 자바 스크립트 태그 (jquery.js, jquery.ujs, application.js 및 기타 그리 관련 JS 파일을 포함) : 난 그냥 내 파괴 경고를 호출하기 위해 노력하고있어
마지막으로.js.erb a 차 create.js.erb로 : 새로 고칠 때
alert("Task was deleted");
다시, CRUD가 정상적으로 작동, 그것은 destroy.js.erb 느낌과 create.js.erb은 어떻게 든 연결되어 있지 않습니다. 몇 가지 힌트를 제공해 주시면 감사하겠습니다.
ActionView::Template::Error (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each):
1:
2: <table>
3: <% @tasks.each do |task| %>
4:
5: <tr>
6: <td><%= best_in_place task, :detail %></td>
app/views/tasks/_tasks.html.erb:3:in `_app_views_tasks__tasks_html_erb___1747031968745406293_70257861453000'
app/views/tasks/destroy.js.erb:4:in `_app_views_tasks_destroy_js_erb___3673072470672404292_70257861472560'
문제가 무엇인지 알아 냈습니다. 생성 및 삭제를 인덱스로 이동 한 후에 추가되지 않은 정의되지 않은 @tasks 변수가 있습니다. 그리고 정의되지 않은 변수로 인해 jquery가 중단되었습니다 (액션이 작동 함에도 불구하고). – Poyi