2014-03-04 6 views
0

나는 며칠 동안 레일 문제에 갇혀 있었고 해결책을 찾지 못했습니다. 내 코드가 작동하는 데 사용되지만 자바 스크립트에 몇 가지 주요 변경 사항 (필수)이 있으며 더 이상 작동하지 않습니다.레일 JQuery POST 요청 대신 GET 보내기

내 사용자는 일정에 참석하고 퇴장 할 수 있습니다. 이것은 사용자가 버튼 클릭을 한 다음 게시 요청을 보내는 것을 취하지 만, 어떤 이유로 요청은 Get 요청으로 처리되고 get 요청에 대한 경로가 없으므로 실패합니다 (게시해야 함).

보기 :

<% if @competition.users.exclude?(@user) %> 
     <%= link_to 'Attend Competition', attend_competition_path(@competition.id), :method => :post %> 
    <% else %> 
     <%= link_to 'Withdraw', withdraw_competition_path(@competition.id), :method => :post %> 

이 application.js

//= require jquery 
//= require jquery_ujs 
//= require turbolinks 
//= require bootstrap 
//= require_tree . 

application.html.erb

<%= javascript_include_tag "application", "data-turbolinks-track" => true %> 
<%= csrf_meta_tags %> 

routes.rb

resources :competitions do 
    post 'attend', on: :member 
    end 
    resources :competitions do 
    member do 
     post 'withdraw' 
    end 
    end 
,

컨트롤러 :

def attend 
@competition = Competition.find(params[:id]) 
if @competition.users.include?(current_user) 
    flash[:error] = "You're already attending this competition." 
elsif current_user.daily == [] 
    flash[:error] = "You must have a working device to compete." 
else 
    current_user.competitions << @competition 
    flash[:success] = "Attending competition!" 
end 
redirect_to @competition 
end 


def withdraw 
p "WITHDRAWING" 
@competition = Competition.find(params[:id]) 
p @competition 
attendee = Attendee.find_by_user_id_and_competition_id(current_user.id, @competition.id) 
if attendee.blank? 
    flash[:error] = "No current attendees" 
else 
    attendee.delete 
    flash[:success] = 'You are no longer attending this competition.' 
end 
p attendee 
redirect_to @competition 
end 

실제 오류 :

ActionController::RoutingError (No route matches [GET] "/competitions/9/withdraw"): 

감사

** EXTRA **

전체 show.view

<p id="notice"><%= notice %></p> 
<p> 
    <%= @competition.title %> 
</p> 
<p> 
    <b>Length:</b> 
    <%= @competition.length %> 
</p> 
    <b>Start:</b> 
    <%= @competition.start.strftime("%d %m %Y") %> 
</p> 
    <b>End:</b> 
    <%= @competition.end.strftime("%d %m %Y") %> 
</p> 
<p><strong>Attendees: </strong> 
    <% if @competition.users.exclude?(@user) %> 
     <%= link_to 'Attend Competition', attend_competition_path(@competition.id), :method => :post %> 
    <% else %> 
     <%= link_to 'Withdraw', withdraw_competition_path(@competition.id), :method => :post %> 
    <% end %> 
</p> 
<% a = 1 %> 
<% for attendee in @competition.users %> 
    <p2><strong><%= a %> </strong><%= link_to attendee.name, attendee %></p2> 
    <% if attendee.daily != [] %> 
    <p><%= attendee.daily.first(7).sum %></p> 
    <% end %> 
    <% a = a + 1 %> 
<% end %> 
</br> 


<%= link_to 'Edit', edit_competition_path(@competition) %> | 
<%= link_to 'Back', competitions_path %> 

HTML 오 utput에서 레일

<a href="/competitions/4/withdraw" data-method="post" rel="nofollow">Withdraw</a> 

** MOAR **

ActionController::RoutingError (No route matches [GET] "/competitions/6/withdraw"): 
    actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' 
    railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app' 
    railties (3.2.3) lib/rails/rack/logger.rb:16:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call' 
    rack (1.4.5) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.4.5) lib/rack/runtime.rb:17:in `call' 
    activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
    rack (1.4.5) lib/rack/lock.rb:15:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call' 
    railties (3.2.3) lib/rails/engine.rb:479:in `call' 
    railties (3.2.3) lib/rails/application.rb:220:in `call' 
    rack (1.4.5) lib/rack/content_length.rb:14:in `call' 
    railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call' 
    rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service' 
    /Users/Marcus/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
    /Users/Marcus/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
    /Users/Marcus/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 


    Rendered /Users/Marcus/.rvm/gems/[email protected]/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms) 

HTML 브라우저에서 로그에서

완전한 오류 :

<a href="/competitions/3/withdraw" data-method="post" rel="nofollow">Withdraw</a> 

전체 페이지 [긴 될 수 있습니다]

<html><head> 
<title>BOTB</title> 
<link data-turbolinks-track="true" href="/assets/application.css" media="all" rel="stylesheet" type="text/css"> 
<script data-turbolinks-track="true" src="/assets/application.js" type="text/javascript"> </script><style type="text/css"></style> 
<meta content="authenticity_token" name="csrf-param"> 
<meta content="Cl7ar8gwVcRsJzrKjp92KB6p6PihvVu0jAwngi18dWU=" name="csrf-token"> 
<!--[if lt IE 9]> 
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> 
<![endif]--> 

    <style id="clearly_highlighting_css" type="text/css">/* selection */ html.clearly_highlighting_enabled ::-moz-selection { background: rgba(246, 238, 150, 0.99); } html.clearly_highlighting_enabled ::selection { background: rgba(246, 238, 150, 0.99); } /* cursor */ html.clearly_highlighting_enabled { /* cursor and hot-spot position -- requires a default cursor, after the URL one */ cursor: url("chrome-extension://pioclpoplcdbaefihamjohnefbikjilc/clearly/images/highlight--cursor.png") 14 16, text; } /* highlight tag */ em.clearly_highlight_element { font-style: inherit !important; font-weight: inherit !important; background-image: url("chrome-extension://pioclpoplcdbaefihamjohnefbikjilc/clearly/images/highlight--yellow.png"); background-repeat: repeat-x; background-position: top left; background-size: 100% 100%; } /* the delete-buttons are positioned relative to this */ em.clearly_highlight_element.clearly_highlight_first { position: relative; } /* delete buttons */ em.clearly_highlight_element a.clearly_highlight_delete_element { display: none; cursor: pointer; padding: 0; margin: 0; line-height: 0; position: absolute; width: 34px; height: 34px; left: -17px; top: -17px; background-image: url("chrome-extension://pioclpoplcdbaefihamjohnefbikjilc/clearly/images/highlight--delete-sprite.png"); background-repeat: no-repeat; background-position: 0px 0px; } em.clearly_highlight_element a.clearly_highlight_delete_element:hover { background-position: -34px 0px; } /* retina */ @media (min--moz-device-pixel-ratio: 2), (-webkit-min-device-pixel-ratio: 2), (min-device-pixel-ratio: 2) { em.clearly_highlight_element { background-image: url("chrome-extension://pioclpoplcdbaefihamjohnefbikjilc/clearly/images/[email protected]"); } em.clearly_highlight_element a.clearly_highlight_delete_element { background-image: url("chrome-extension://pioclpoplcdbaefihamjohnefbikjilc/clearly/images/[email protected]"); background-size: 68px 34px; } } </style><style>[touch-action="none"]{ -ms-touch-action: none; touch-action: none; }[touch-action="pan-x"]{ -ms-touch-action: pan-x; touch-action: pan-x; }[touch-action="pan-y"]{ -ms-touch-action: pan-y; touch-action: pan-y; }[touch-action="scroll"],[touch-action="pan-x pan-y"],[touch-action="pan-y pan-x"]{ -ms-touch-action: pan-x pan-y; touch-action: pan-x pan-y; }</style></head> 
    <body style=""> 
    <header class="navbar navbar-fixed-top navbar-inverse"> 
    <div class="navbar-inner"> 
    <div class="container"> 
     <a href="/" id="logo">Battle Of The Bands</a> 
     <nav> 
     <ul class="nav pull-right navbar-nav " id="menu"> 
      <li><a href="/">Home</a></li> 
      <li><a href="/competitions">Competition</a></li> 
      <li><a href="/users">Users</a></li> 
      <li id="fat-menu" class="dropdown"> 
       <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 
       Account <b class="caret"></b> 
       </a> 
       <ul class="dropdown-menu"> 
       <li><a href="https://stackoverflow.com/users/1">Profile</a></li> 
       <li><a href="/devices">Connect Device</a></li> 
       <li><a href="/competitions/new">Create Competition</a></li> 
       <li><a href="https://stackoverflow.com/users/1/edit">Settings</a></li> 
       <li class="divider"></li> 
       <li> 
        <a href="/signout" data-method="delete" rel="nofollow">Sign out</a> 
       </li> 
       </ul> 
      </li> 
     </ul> 
     </nav> 
    </div> 
    </div> 
</header> 
    <div class="container"> 
     <p id="notice"></p> 

<p> 
    Tester March 8 
</p> 

<p> 
    <b>Length:</b> 
    5 
</p> 
    <b>Start:</b> 
    03 03 2014 
    <p></p> 
    <b>End:</b> 
    03 03 2014 
<p></p> 
<p><strong>Attendees: </strong> 
     <a href="/competitions/3/withdraw" data-method="post" rel="nofollow">Withdraw</a> 
</p> 
    <p2><strong>1 </strong><a href="https://stackoverflow.com/users/1">Marcus Smith</a></p2> 
    <p>49774</p> 
<br> 


<a href="/competitions/3/edit">Edit</a> | 
<a href="/competitions">Back</a> 

     <footer class="footer"> 
    <small> 
    by <a href="http://www.twitter.com/_mhsmith_"> Marcus Smith</a> 
    </small> 
    <nav> 
    <ul> 
     <li><a href="/about">About</a></li> 
    </ul> 
    </nav> 
</footer> 
     <!-- <pre class='debug_dump'>--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
action: show 
controller: competitions 
id: '3' 
    </pre> --> 
    </div> 

</body></html> 
+1

철수 링크에 대해 결과 HTML을 공유 할 수 있습니까? Rails가 그 일을한다면, 그것은 형태 일 것입니다. 부수적으로 (그리고 아마도 복사/붙이기) 당신은 당신의 관점에서 ** 끝 ** 없다. 모든 것이 올바르게 보이기 때문에 언급하는 것이므로 이상한 물건을 찾아야합니다. – Troy

+0

추가 정보가 추가되었습니다. 잘하면이 도움이 – Marcus

+1

페이지에 어떤 자바 스크립트 오류있어? – sevenseacat

답변

0

jquery_ujs.js가 없기 때문에 이것이 의심 스럽습니다. 당신의 application.js 매니페스트에서

// application.js 
// 
//= require jquery 
//= require jquery_ujs 

, 당신은 jquery_ujs을 포함해야한다 - 때문에 태그의 data-method 필드의 POST로 GET 요청을 변환합니다 눈에 거슬리지 자바 스크립트 코드 it't.

당신은이 jquery_ujs.js이 m 다음 자바 스크립트 오류에 대한 sevenseacat의 발언은 아마 포함해야합니까 경우 - 당신이 실행되는 jquery_ujs.js 전에 제기되고있는 자바 스크립트 오류가 있다면, 그것은 할 수 없습니다 귀하의 링크를 해석하십시오.

+0

내 application.js 파일에 jquery_ujs가 포함되어 있습니다. 로그에서 전체 오류를 포함 시켰습니다. 아마도 도움이 될 수 있습니다. – Marcus

0

질문을 한 후에 많은 시간이 경과했기 때문에 누군가를 볼 수 있을지 모르겠지만 같은 문제가 발생했습니다. 따라서 누군가가 답변을 찾고 jquery-ujs가 문제가 아니 었습니다. 코드에서 어디

$(document).unbind('click') ...

: 더블 코드를 확인하고 당신이하지 가지고 할 확인 - 여기에 마지막으로 나를 위해 그것을 해결거야. 이로 인해 Rails가 GET 요청을 POST 또는 DELETE 요청으로 변환하여 실행되지 않게하는 데 필요한 javascript가 발생합니다. 희망은 다른 누군가를 위해 아주 오랫동안 나를 도청 한이 문제를 해결하는 데 도움이됩니다.