ARTLoe
ARTLoe

Reputation: 1799

Preventing multiple POSTS in Ruby on Rails app - Rails 4

I am unsure what is happening.

can one kindly tell me how to prevent this?

multiple post displayed in terminal:

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]

schema:

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

attendance.rb

class Attendance < ActiveRecord::Base
  belongs_to :user
  belongs_to :attendable, polymorphic: true
end

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

views file:

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

Upvotes: 0

Views: 25

Answers (1)

Dmitry Kukhlevsky
Dmitry Kukhlevsky

Reputation: 276

You could use this:

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

given a user can attend each event only once

Upvotes: 1

Related Questions