2014-04-07 2 views
0

나는이처럼 보이는 컨트롤러 방법에 대한 'POST'를 요청하기 위해 노력하고있어 :내 'POST'요청이 내 방법을 보내지 않는 이유는 무엇인가요?

$.ajax({type: "POST", url: something, data: {to_find_id: 978636122}, success:function(){ }}); 

은 다음 로그 보여줍니다 내 데이터, 전송 :

Started POST "/thingys/519716477/do_something" for 127.0.0.1 at 2014-04-03 14:54:44 +0200 
Processing by ThingysController#do_something as */* 
    Parameters: {"to_find_id"=>"978636122", "thingy_id"=>"519716477"} 
    ←[1m←[36mUser Load (0.0ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "users"."id" =? LIMIT 1←[0m [["id", 702273327]] 
Redirected to http://localhost:3000/ 
Completed 302 Found in 0ms (ActiveRecord: 0.0ms) 

의 간단한 설명을 302 Found : WIKIPEDIA-LINK

내 방법 :

def do_something 
    Something.create(timespan_id: params[:to_find_id].to_s, thingy_id: @thingy.id) 
    respond_to do |format| 
    format.js {render :nothting => true} 
    end 
end 

routes.rb- 파일에 내 방법을 이미 정의했습니다.

resources :thingys do 
    post 'do_something' 
    post 'undo_something' 
end 

그래서 요청을 보내고 심지어 방법을 찾습니다.

내가 그것을 어떻게 작동시킬 수 있는지 알고 있니?


편집 :

내 정품 토큰 추가했습니다,하지만 난 여전히 내 방법에 액세스 할 수 없습니다.

Here is what I get now: 
Started POST "/thingys/519716477/add_something?&authenticity_token=1l3tTWfwNgt56lp93w2QghUJvUYZn87V/8Zs7Fcrijq+8=" for 127.0.0.1 at 2014-04-07 13:04:15 +0200 
Processing by ThingysController#add_something as */* 
    Parameters: {"to_find_id"=>"310026847", "authenticity_token"=>"1l3tTWfwNgt56lp93w2QghUJvUYZn87V/8Zs7Fcrijq 8=", "subgroup_id"=>"519716477"} 
    ←[1m←[35mUser Load (0.5ms)←[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 702273327]] 
    ←[1m←[36mThingyp Load (0.5ms)←[0m ←[1mSELECT "subgroups".* FROM "thingys" WHERE "thingys"."id" = ? LIMIT 1←[0m [["id", "519716477"]] 
Redirected to http://localhost:3000/ 
Completed 302 Found in 22ms (ActiveRecord: 1.0ms) 

나는 또한 나의 ThingysController 내와 ApplicationController에 protect_from_forgery with: :exceptionskip_before_filter:verify_authenticity_token을 추가했습니다.

+0

에 무엇을 작동하지 않는 이유는 무엇입니까? 너는 무엇을 기대 하느냐? 대신 무엇이 발생합니까? –

답변

0

"작동하지 않음"은 작업이 예상 작업을 수행하지 않고있는 것으로 가정합니다.이 작업은 Something 인스턴스를 만드는 것입니다. 추측해야만한다면 모델에 정의 된 유효성 검사를 통과하지 못했습니다. 그러면 모델이 저장되지 않습니다.

시도 할 수 있습니다 : Something.create!(attrs) 대신 유효성 검사에 실패하면 오류가 발생하여 저장 이유가 명확 해집니다.

작동하려면 응용 프로그램에 해당하는 유효한 Something을 저장하면됩니다.


당신은 또한 당신이 레일에서 인증 토큰없이 레일에 POST의 PARAM을 보내 질수 오타에게

format.js {render :nothting => true} 
#     ^typo! 
+0

Hello Alex, 빠른 응답에 감사드립니다. 아무 것도하지 않습니다. 나는 코드를 추가하려고 시도했다. 메서드를 호출하면,'puts "로 라인을 추가하는 것과 같다. ----- IT WORKS -----" "내 메소드에는 있지만 인쇄는하지 않는다. . 그래서 그것은 내 방법을 실행하지 않습니다. – user3383458

1

있습니다.

이 페이지에서 CSRF 대책을 참조하십시오 http://guides.rubyonrails.org/security.html

또는 스레드 Understanding the Rails Authenticity Token

+0

내 Ajax 요청과 함께 앱에 인증 토큰을 보내려면 어떻게해야합니까? – user3383458

+0

이 참조 http://stackoverflow.com/questions/7560837/proper-way-to-send-an-authenticity-token-with-ajax –

+0

고맙습니다. 나는 이미 인증 코드를 가지고 있지만, 여전히 그렇지 않습니다. 작업. 나는 새로운 편집을 기록에 넣었습니다 – user3383458

관련 문제