2017-01-27 3 views
0

何が起きているのかよく分かりません。Ruby on Railsアプリケーションで複数のPOSTSを防止する - Rails 4

  • 私はattending buttonをクリックしてイベントに参加することができます。
  • attend buttonをユーザーがクリックすると私は私の端末で複数のPOSTリクエストを取得
  • これは、イベントに出席したユーザを表示する3回
  • これが唯一のFirefoxブラウザで起こる

することができます私は親切にこれを防ぐ方法を教えてください?

端末に表示された複数のポスト:

Started POST "/attending_socials?social_id=new-members-night-out-west-london" for 127.0.0.1 at 2017-01-27 13:56:26 +0000 
Processing by AttendingSocialsController#create as HTML 
    Parameters: {"authenticity_token"=>"G/9QyoKBaTTsivCKmcKAFO7RoPC6B7Lm6C4G7er2pGYIjYirJwcCUDXCrCoODk5tPl3cRMQUZM6fouQrLnvjRg==", "social_id"=>"new-members-night-out-west-london"} 
    Social Load (0.1ms) SELECT "socials".* FROM "socials" WHERE "socials"."slug" = ? ORDER BY "socials"."id" ASC LIMIT 1 [["slug", "new-members-night-out-west-london"]] 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 20]] 
    (0.1ms) begin transaction 
    SQL (0.5ms) INSERT INTO "attendances" ("attendable_id", "attendable_type", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["attendable_id", 11], ["attendable_type", "Social"], ["user_id", 20], ["created_at", "2017-01-27 13:56:26.450155"], ["updated_at", "2017-01-27 13:56:26.450155"]] 
    SQL (0.3ms) INSERT INTO "activities" ("parameters", "key", "owner_id", "owner_type", "trackable_id", "trackable_type", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["parameters", nil], ["key", "attendance.create"], ["owner_id", 20], ["owner_type", "User"], ["trackable_id", 156], ["trackable_type", "Attendance"], ["created_at", "2017-01-27 13:56:26.457301"], ["updated_at", "2017-01-27 13:56:26.457301"]] 
    (2.9ms) commit transaction 
Redirected to http://localhost:3000/socials/new-members-night-out-west-london 
Completed 302 Found in 19ms (ActiveRecord: 4.1ms) 


Started POST "/attending_socials?social_id=new-members-night-out-west-london" for 127.0.0.1 at 2017-01-27 13:56:26 +0000 
Processing by AttendingSocialsController#create as HTML 
    Parameters: {"authenticity_token"=>"G/9QyoKBaTTsivCKmcKAFO7RoPC6B7Lm6C4G7er2pGYIjYirJwcCUDXCrCoODk5tPl3cRMQUZM6fouQrLnvjRg==", "social_id"=>"new-members-night-out-west-london"} 
    Social Load (0.2ms) SELECT "socials".* FROM "socials" WHERE "socials"."slug" = ? ORDER BY "socials"."id" ASC LIMIT 1 [["slug", "new-members-night-out-west-london"]] 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 20]] 
    (0.1ms) begin transaction 
    SQL (0.4ms) INSERT INTO "attendances" ("attendable_id", "attendable_type", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["attendable_id", 11], ["attendable_type", "Social"], ["user_id", 20], ["created_at", "2017-01-27 13:56:26.488498"], ["updated_at", "2017-01-27 13:56:26.488498"]] 
    SQL (0.3ms) INSERT INTO "activities" ("parameters", "key", "owner_id", "owner_type", "trackable_id", "trackable_type", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["parameters", nil], ["key", "attendance.create"], ["owner_id", 20], ["owner_type", "User"], ["trackable_id", 157], ["trackable_type", "Attendance"], ["created_at", "2017-01-27 13:56:26.497129"], ["updated_at", "2017-01-27 13:56:26.497129"]] 
    (1.0ms) commit transaction 
Redirected to http://localhost:3000/socials/new-members-night-out-west-london 
Completed 302 Found in 19ms (ActiveRecord: 2.1ms) 


Started POST "/attending_socials?social_id=new-members-night-out-west-london" for 127.0.0.1 at 2017-01-27 13:56:27 +0000 
Processing by AttendingSocialsController#create as HTML 
    Parameters: {"authenticity_token"=>"G/9QyoKBaTTsivCKmcKAFO7RoPC6B7Lm6C4G7er2pGYIjYirJwcCUDXCrCoODk5tPl3cRMQUZM6fouQrLnvjRg==", "social_id"=>"new-members-night-out-west-london"} 
    Social Load (0.2ms) SELECT "socials".* FROM "socials" WHERE "socials"."slug" = ? ORDER BY "socials"."id" ASC LIMIT 1 [["slug", "new-members-night-out-west-london"]] 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 20]] 
    (0.1ms) begin transaction 
    SQL (0.3ms) INSERT INTO "attendances" ("attendable_id", "attendable_type", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["attendable_id", 11], ["attendable_type", "Social"], ["user_id", 20], ["created_at", "2017-01-27 13:56:27.510657"], ["updated_at", "2017-01-27 13:56:27.510657"]] 
    SQL (0.1ms) INSERT INTO "activities" ("parameters", "key", "owner_id", "owner_type", "trackable_id", "trackable_type", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["parameters", nil], ["key", "attendance.create"], ["owner_id", 20], ["owner_type", "User"], ["trackable_id", 158], ["trackable_type", "Attendance"], ["created_at", "2017-01-27 13:56:27.516075"], ["updated_at", "2017-01-27 13:56:27.516075"]] 
    (2.7ms) commit transaction 
Redirected to http://localhost:3000/socials/new-members-night-out-west-london 
Completed 302 Found in 16ms (ActiveRecord: 3.5ms) 

route.rb:

resources :attending_socials, only: [:create, :destroy] 

スキーマ:

ActiveRecord::Schema.define(version: 20170121184409) do 

    create_table "attendances", force: :cascade do |t| 
    t.integer "attendable_id" 
    t.string "attendable_type" 
    t.integer "user_id" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    end 

    add_index "attendances", ["attendable_type", "attendable_id"], name: "index_attendances_on_attendable_type_and_attendable_id" 
    add_index "attendances", ["user_id"], name: "index_attendances_on_user_id" 
end 

012 attendance.rb

attending_socials_controller.rb

class AttendingSocialsController < ApplicationController 
    before_action :set_social 

    def create 
    if Attendance.create(attendable: @social, user: current_user) 
     redirect_to :back, notice: 'Attending Social' 
    else 
     redirect_to :back, alert: 'Something went wrong...*sad panda*' 
    end 
    end 


    def destroy 
    Attendance.where(attendable_id: @social.id, user_id: current_user.id).first.destroy 
    redirect_to :back, notice: 'Not Attending Social' 
    end 

    private 
    def set_social 
    @social = Social.friendly.find(params[:social_id] || params[:id]) 
    end 
end 

ビューファイル:

<div><%= link_to 'Attend testing', attending_socials_path(social_id: @social), method: :post %></div> 
+0

を各イベントに出席することができ与えられましたページをチェックインしてくださいあなたが 'jquery_ujs'を二度追加した場合は、 –

+0

をチェックしてください。それは一度だけ含まれています – ARTLoe

答えて

1

あなたはこれを使用することができます:

Attendance.find_or_create_by(attendable: @social, user: current_user) 

ユーザーが一度だけ

+0

すごい!驚くばかり!驚くばかり!あなたにすごく感謝!!!!!!!!!!! @ドミトリー – ARTLoe

関連する問題