2014-12-22 4 views
2

거기에 많은 답변이있을 수 있지만 거기에 내 요구에 맞게 해석 할 수 없습니다. 내 모델에서 아약스를 사용하여 레코드 특성을 업데이트 할 찾고 있습니다. 내 경우아약스 호출 속성을 업데이트하십시오.

선택한 속성 : 진정한

내 시나리오에서

내가 요소를 클릭하고 업데이트 할 찾고 그 모델의 요소 기록

<ul class="components"> 
<% @component.each do |c| %> 
    <li id="<%= c.component_name %>" data-id="<%= c.id %>" data-remote="true"><%= c.component_name %></li> 
<% end %> 
</ul> 

jQuery를

$('#Literacy, #Numeracy').on('click', function(){ 

    var component_name = $(this).text(); 
    var component_id = $(this).data('id'); 
    data_send = { id: component_id } 

    $.ajax({ 
    method: 'put', 
    url: '/components/selected_component', 
    data: data_send, 
    success: function(data) { 
    console.log(data) 
    } 
}); 
}); 

컨트롤러

def selected_component 
@component = Component.find(params[:id]) 
@component.update_attributes(selected: true) 
end 

어쩌면 잘못이보고 메신저하지만 난 클릭 이벤트를 통해 ID를 얻을 수있는 방법이 순간 내 selected_component 방법

임 여기에 몇 비트 누락하지만 고민 오늘

로 (component_id로 저장) 내가 오류

Missing template components/selected_component 

을 얻고있다하지만 난 업데이트 성공 후 해당 페이지를 렌더링 할 해달라고, 난 그냥 같은 페이지

편집

에 있고 싶어 내 문제를 해결하기

난 그냥 내 format.js

{ render nothing: true } 

답변

1

누락 된 템플릿 오류는 자바 스크립트 요청을 얻을 때 무엇을 알고있는 뷰를 가지고 있지 대응이를 추가했다.

views/components/selected_components.js.erb 파일을 만들면 오류가 해결됩니다.

+0

답장을 보내 주셔서 감사합니다. 클릭 이벤트로이 작업을 수행하고 싶습니다. 현재 오류가 발생하여 질문을 업데이트했습니다. – Richlewis

+0

다음에 다른 오류가 발생하면 더 많은 오류가 표시되고 업데이트됩니다. 다시 연락 드리겠습니다. – theWanderer4865

+0

고마워, 지금 일하고있어 :) – Richlewis