2017-12-14 6 views
0

「友達を追加」リンクをクリックすると、2つの同一のレコードが作成されます。 1つのレコードしか作成されていないはずです。私はcreateメソッドにリンクするさまざまな方法を根本的な問題として考えました。2つのレコードを1つではなく作成するメソッドを作成しますか?

ビュー:ユーザーが/インデックス

<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

は 'create'方法は、問題の原因ではありません。しかし、アクションを作成する要求は2回続いています。 –

+0

もしそれがヒントだったら私は別のものが必要です。私はlink_toがおそらく2つのリクエストを送ることができるか見ていないのですか? –

+1

'link_to xxx、method::post'がjQueryによって処理されたので、あなたのjQueryコードに何か問題がありました。 – yeuem1vannam

答えて

1

JSは自分のアプリケーションの中に二回ロードされていました。私のapplication.jsファイルからこれらの行を削除すると、リクエストは1つしか作成されませんでした。

削除:

//= require turbolinks 
//= require rails-ujs 
//= require_tree . 

電流:

//= require jquery 
//= require jquery.turbolinks 
//= require jquery_ujs 
//= require bootstrap/dropdown 
+1

これはありがとう! jquery-ujs JSが私にレコードの重複を引き起こしていました。 – gitastic

関連する問題