2013-06-09 2 views
1

현재보기에서 버튼을 클릭하여 데이터베이스의 폴링 is_live 속성을 전환하려고합니다. polls 데이터를 params로 보내지 않는 폼과 클릭이 있습니다. PollsController에서이 작업에 대한 링크AJAX를 사용하여 데이터베이스의 부울 열을 전환하는 방법?

- current_user.polls.each do |poll| 
    %tr#poll.id 
     %td 
     /Need to add (poll) as it needs the ID to match to the route. 
     =link_to "#{poll.title}", poll_path(poll) 
     %td 
     =link_to "Edit", edit_poll_path(poll) 
     %td 
     - if poll.is_live 
      = link_to "Stop" 
     /, post, :method=>:toggle_live, :remote=>true, :class => 'btn btn-danger stop-poll' 
     - else 
      **= link_to "Start", polls_toggle_live_path(poll), :method => :post, :remote => true, :locals => poll, :class=> 'btn btn-success btn-small start-poll'** 

내가 레일 C에서 이것을 테스트했습니다 폴링 모델에서이 방법으로 연결하고 토글 스위치 않습니다

def toggle_live 
    binding.pry 

    @poll = Poll.find(params[:id]) 

    respond_to do |format| 
     **format.js {@poll.toggle_live}** 
    end 

    end 

부울 값

def toggle_live 
    if self.is_live 
     self.is_live = false 
    else 
     self.is_live = true 
    end 
    end 

이러한 모든 것을 어떻게 사용할 수 있습니까? 클릭 이벤트로 부울을 전환 하시겠습니까?
는 현재 내가 서버 로그에서이 오류를 받고 있어요 : 404에 대한

Started POST "/polls/toggle_live.30" for 127.0.0.1 at 2013-06-09 12:15:02 -0400 
Processing by PollsController#toggle_live as 
Completed 404 Not Found in 2447731ms 

답변

2

이유는 당신의 방법을 넣어 게시하지해야한다는 것입니다. 길을 따라, 나는 모델에 제안 할 수 있습니다 :

def toggle_live 
    self.is_live = !is_live 
end 

와 같은 컨트롤러 뭔가

:

def toggle_live 
    @poll = Poll.find params[:id] 
    # do something to verify the user has the right to toggle this poll 
    @poll.toggle_live 
end 

요청 이후 원격에 의해 이루어집니다 : 사실, 당신은 레일이 자동으로 렌더링 할 것이라는 점을 알고있다 JS 템플릿이므로 respond_to는 불필요합니다.