首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >防止Rails应用程序- Rails 4中的多个帖子

防止Rails应用程序- Rails 4中的多个帖子
EN

Stack Overflow用户
提问于 2017-01-27 14:11:11
回答 1查看 25关注 0票数 0

我不知道发生了什么事。

  • 我有一个attending button,用户可以点击它来参加一个活动。
  • 当用户单击attend button时,我会在终端中获得多个post请求。
  • 这将显示参加该事件的用户3次。
  • 这种情况只发生在firefox浏览器中。

有人能告诉我如何防止这种情况吗?

在终端中显示的多个帖子:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
resources :attending_socials, only: [:create, :destroy]

模式:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
class Attendance < ActiveRecord::Base
  belongs_to :user
  belongs_to :attendable, polymorphic: true
end

attending_socials_controller.rb

代码语言:javascript
运行
复制
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

视图文件:

代码语言:javascript
运行
复制
<div><%= link_to 'Attend testing', attending_socials_path(social_id: @social), method: :post %></div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-27 14:32:59

你可以用这个:

代码语言:javascript
运行
复制
Attendance.find_or_create_by(attendable: @social, user: current_user)

假设用户只能参加每次活动一次

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41895935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档