当我点击链接"添加朋友"时,会生成2个相同的记录。只应创建一条记录。我已经尝试了不同的方式链接到create
方法,因为它可能是潜在的问题。
查看:users / index
<ul>
New friend requests
<% @incoming.each do |user| %>
<% @users.each do |f| %>
<% if f.id == user.user.id %>
<li>
<%= f.name %>
<%= link_to "Accept request", friend_request_path(id: user), :method => :put %>
</li>
<% end %>
<% end %>
<% end %>
</ul>
<ul>
All users
<% @users.each do |user| %>
<% if user.id != current_user.id %>
<li><%= user.name %>
<%= link_to "Add Friend", friend_requests_path(:friend_id => user), :method => :post %></li>
<% end %>
<% end %>
</ul>
控制器:
class FriendRequestsController < ApplicationController
before_action :set_friend_request, except: [:index, :create]
def index
@incoming = FriendRequest.where(friend: current_user)
@outgoing = current_user.friend_requests
end
def create
friend = User.find(params[:friend_id])
@friend_request = current_user.friend_requests.new(friend: friend)
if @friend_request.save
flash[:notice]="Friend request sent."
redirect_to users_path
else
flash[:alert]="Friend request not sent."
redirect_to root_path
end
end
def update
@friend_request.accept
flash[:notice]="Friend added!"
redirect_to root_path
end
def destroy
@friend_request.destroy
head :no_content
end
private
def set_friend_request
@friend_request = FriendRequest.find(params[:id])
end
end
日志:
Started POST "/friend_requests?friend_id=2" for 127.0.0.1 at 2017-12-13 23:55:26 -0500
Processing by FriendRequestsController#create as HTML
Parameters: {"authenticity_token"=>"+bCpezt22p+Jt3fmdbb66tIUrSJgsyfTpLbY2WHsZo9DAfwV+hxEr6GRU7nwESTPX6Oqz56GuikiG+qhjFhVcQ==", "friend_id"=>"2"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.4ms) BEGIN
Started POST "/friend_requests?friend_id=2" for 127.0.0.1 at 2017-12-13 23:55:26 -0500
Processing by FriendRequestsController#create as HTML
SQL (4.3ms) INSERT INTO "friend_requests" ("user_id", "friend_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["user_id", 1], ["friend_id", 2], ["created_at", "2017-12-14 04:55:26.660451"], ["updated_at", "2017-12-14 04:55:26.660451"]]
Parameters: {"authenticity_token"=>"+bCpezt22p+Jt3fmdbb66tIUrSJgsyfTpLbY2WHsZo9DAfwV+hxEr6GRU7nwESTPX6Oqz56GuikiG+qhjFhVcQ==", "friend_id"=>"2"}
(8.3ms) COMMIT
Redirected to http://localhost:3000/users
Completed 302 Found in 64ms (ActiveRecord: 14.3ms)
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(2.8ms) BEGIN
SQL (2.8ms) INSERT INTO "friend_requests" ("user_id", "friend_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["user_id", 1], ["friend_id", 2], ["created_at", "2017-12-14 04:55:26.698180"], ["updated_at", "2017-12-14 04:55:26.698180"]]
(2.5ms) COMMIT
Redirected to http://localhost:3000/users
Completed 302 Found in 39ms (ActiveRecord: 18.7ms)
答案 0 :(得分:1)
JS被加载到我的应用程序中两次。从我的application.js文件中删除这些行只导致一个请求。
移除:
//= require turbolinks
//= require rails-ujs
//= require_tree .
电流:
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require bootstrap/dropdown