Rails + チョロQのindex.rhtmlがどうしようもない件
公開日:
:
最終更新日:2014/01/31
Ruby on Rails
記事内に広告を含む場合があります。記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。
app/views/top/index.rhtml
function key_q(){
new Ajax.Request('/top/frontleft', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('1ec5a628d51415de21cdf90e02be46b57aed18a5')}); return false;
}
authenticity_tokenを直書き。なんじゃそりゃ。
authenticity_tokenについて調査
じゃあ、どこで設定されているか、というと、app/controllers/application.rb をご覧ください。
protect_from_forgery # :secret => ‘6d0b20bbf9203508337aff3214f79efb7a0’
protect_from_forgery と書くだけです。終わり。
そうすると、POST・DELETE・PUTの時(つまりGETじゃあないとき)に、Rails が、authenticity_tokenという hidden パラメータのようなものを付け加えてくれてこれでチェックされます。
なるほど。
app/controllers/application.rb
# protect_from_forgery # :secret => '1b2ae027af57fa988abff42920360cda'
よし、コメントアウトした。
app/views/top/index.rhtml
function key_q(){
new Ajax.Request('/top/frontleft', {asynchronous:true, evalScripts:true}); return false;
}
ひとまずはこれでOKかな??
動作確認
よし!!AJAXでリレーが動いた!!