4

render_to_string을 통해 작은 양식을 생성하고 있으며 CSRF 토큰이 올바르게 생성되지 않습니다. 즉, 헤더와 다른데 사용자가 제출시 함께 로그 아웃됩니다. 로그에서 "CSRF 토큰을 확인할 수 없습니다"). 여기에 관련 코드입니다 :정품 인증 토큰이 render_to_string을 사용할 때 다르게 생성됩니다.

컨트롤러 :

def publish 
    @question = @event.questions.find(params[:id]) 
    @question.update_attribute(:published, true) unless @question.published? 
    Pusher[@event.to_param].trigger('new_question', question: render_question) 
    redirect_to event_path(@event) 
end 


private 
    def render_question 
    render_to_string('questions/_unanswered_question', locals: {question: @question}, layout: false) 
    end 
    def fetch_event 
    @event ||= current_user.events.find(params[:event_id]) 
    end 

내가 미는을 사용하고 있지만,이 그냥이 자바 스크립트로 페이지에 렌더링되는 가정 할 수있다 :

$("#questions").append(data.question); // data is what I send from Pusher. 

을 그리고 마지막으로, 렌더링 된 부분 :

.answer 
    = form_for [@event, question, question.answers.new] do |f| 
    %h2 
     = question.title 

    %ul 
     - (1..5).each do |n| 
     - if question.send("answer_#{n}").present? 
      %li 
      = f.radio_button :option, n, id: "q_#{question.id}_answer_option_#{n}" 
      = f.label question.send("answer_#{n}"), for: "q_#{question.id}_answer_option_#{n}" 

    %p 
     = f.submit "Answer" 

이것은 파인더에 추가되지 않고 잘 동작합니다. ge이지만 레이아웃 내에서 렌더링됩니다. 이것은 원격 양식이 아닙니다.

+0

당신은 당신의'application.html.erb' 파일의 코드를 게시 할 수 있습니다 부디? 머리 부분이 필요 '이 부분 ...'또는 crf_token 태그가 있는지 저에게 말할 수 있습니까? – rmagnum2002

+0

'csrf_meta_tags'가 있습니다. –

+1

사용자를 기록하는 요청의 매개 변수를 게시 할 수 있습니까? 요청 직후에 로그 파일에서 전송 된 매개 변수를 찾을 수 있습니다. –

답변

0

아약스를 사용하여 양식을 생성 한 다음 Dom에 추가하는 것처럼 보입니다. csrf 토큰은 모든 요청마다 새로 생성됩니다. 내가 아는 한, 헤더에서 사용할 수있는 csrf 토큰을 사용하고 js를 사용하여 양식의 토큰을 바꿔야합니다.

은 다음과 같이 보일 것이다 : 현재

success:()-> 
    parameter = $("meta[name=csrf-param]").attr("content") 
    token  = $("meta[name=csrf-token]").attr("content") 
    question = $(data.question).find("[name="+parameter+"]").val(token) 

내가 먼저 테스트 할 컴퓨터가 없기 때문에 잘하면 내가 맞다 :/

관련 문제