###ゲーム内容
① データベースに格納されているx枚の画像から、
2枚をランダムに抽出し、並べて表示させます。(@target1, @target2として定義。)
画像はElorateカラムを持っていてデフォルトの数値は1500です。
② クリックした画像のElorateを公式に基づき更新させ、
同様にクリックしなかった画像のElorateも更新させる。
###ソースコード
def index @photoposts = Photopost.find(Photopost.pluck(:id).shuffle[0..1]) @target1 = @photoposts[0] @target2 = @photoposts[1] end . . . def vote @target1 = Photopost.find(params[:target1]) @target2 = Photopost.find(params[:target2]) @ra = @target1.elorate @rb = @target2.elorate @ea = 1 / ( 1 + ( 10 ** ((@rb - @ra) / 400))) @eb = 1 / ( 1 + ( 10 ** ((@ra - @rb) / 400))) @rawin = @ra + 16 * (1 - @ea) @ralose = @ra + 16 * (0 - @ea) @rbwin = @rb + 16 * (1 - @eb) @rblose = @rb + 16 * (0 - @eb) if params[:target1] == params[:id] @target1.elorate = @rawin @target1.save @target2.elorate = @rblose @target2.save redirect_to photoposts_path elsif params[:target2] == params[:id] @target1.elorate = @ralose @target1.save @target2.elorate = @rbwin @target2.save redirect_to photoposts_path end end
<% @photoposts.each do |photopost| %> <%= link_to image_tag(photopost.photo_url), photoposts_path(:id => photopost.id,:target1 => @target1,:target2 =>@target2), :method => "patch" %> <%= photopost.elorate %> <% end %>
###起きている問題
Elorate1500(画像id:1) VS Elorate1500(画像id:2)で、
画像id:1をクリックしたらElorate1516になったのですが、
画像id:2のElorateが更新されませんでした。
私はなぜ正しく実装できなかったのでしょうか?
回答宜しくお願いします。
###追記
Elorateを使用せずにクリックした画像に+=1, しなかった画像に-=1にした場合は正常に動作します。
なので数式の書き方に問題があるのでしょうか?
ご教授願います。。。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。