2016-12-17 82 views
1

我得到下面的錯誤,並不確定如何更正它 - 你的幫助將不勝感激。沒有路線匹配{:action =>「update」,:controller =>「」 - Rails 5

當在用戶顯示頁面,我不明白的錯誤,但是當在例如在任何其他網頁...如事件index.html頁面錯誤顯示

No route matches {:action=>"update", :controller=>"friendships", :friend_id=>#<User id: 5, email: "[email protected]" 

擴展錯誤在終端

Started GET "/events" for ::1 at 2016-12-17 12:44:43 +0000 
Processing by EventsController#index as HTML 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 4]] 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 4]] 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."email" IS NULL LIMIT 1 
    Subscription Load (0.1ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."title" = ? LIMIT 1 [["title", "premium"]] 
    User Load (0.1ms) SELECT "users".* FROM "users" INNER JOIN "friendships" ON "users"."id" = "friendships"."friend_id" WHERE "friendships"."user_id" = ? AND "friendships"."status" = ? [["user_id", 4], ["status", "requested"]] 
    (0.2ms) SELECT COUNT(*) FROM "users" INNER JOIN "friendships" ON "users"."id" = "friendships"."friend_id" WHERE "friendships"."user_id" = ? AND "friendships"."status" = ? [["user_id", 4], ["status", "requested"]] 
    Rendered shared/_content_dropdownbox_friendrequest.html.erb (4.8ms) 
    Rendered shared/_header.html.erb (10.7ms) 
    Rendered events/index.html.erb within layouts/application (11.8ms) 
Completed 500 Internal Server Error in 23ms 

ActionController::UrlGenerationError - No route matches {:action=>"update", :controller=>"friendships", :friend_id=>#<User id: 5, email: "[email protected]", encrypted_password: "$2a$10$PMO5FPBzjjnFHI/ye8rfP.ONtHP3gagXomj1sbruBXH..." 

路線文件

Rails.application.routes.draw do 

    resources :friendships, only: [:create, :update, :destroy] 

    devise_for :users 
    resources :users 
end 

視圖/共享/ _content_dropdownbox_friendrequ est.html.erb

<ul> 
    <% current_user.requested_friends.each do |requester| %> 
    <li> 
     <%= link_to(image_tag(requester.image_url, :alt => "image", :class =>"#", id: ""), user_path(requester)) %> 
     <%= link_to truncate("#{requester.firstname} #{requester.lastname}", length: 23), user_path(requester) %> 
     <%= link_to 'Accept', friendship_path(user_id: current_user, friend_id: requester), controller: "friendships", action: "update", method: :put %> 
     <%= link_to 'Decline', friendship_path(user_id: current_user, friend_id: requester), controller: "friendships", action: "decline", method: :delete %> 
    </li> 
    <div class="clear"></div> 
    <% end %> 
</ul> 

frienships_controller

class FriendshipsController < ApplicationController 
    before_action :authenticate_user! 
    before_filter :setup_friends 

    # Sends a friend request. 
    # We'd rather call this "request", but that's not allowed by Rails. 
    def create 
    Friendship.request(@user, @friend) 
    flash[:notice] = "Request sent." 
    redirect_to :back 
    end 

    # Accepts a friend request. 
    # We'd rather call this "accept", but that's not allowed by Rails. 
    def update 
    @user = User.friendly.find(params[:user_id]) 
    @friend = User.friendly.find(params[:friend_id]) 
    if @user.requested_friends.include?(@friend) 
     Friendship.accept(@user, @friend) 
     flash[:notice] = "Connection with #{@friend.firstname} accepted!" 
    else 
     flash[:notice] = "No connect request from #{@friend.firstname}." 
    end 
    redirect_to :back 
    end 

    def destroy 
    @user = User.friendly.find(params[:user_id]) 
    @friend = User.friendly.find(params[:friend_id]) 
    if @user.requested_friends.include?(@friend) #decline 
     Friendship.breakup(@user, @friend) 
     redirect_to :back 
    elsif @user.pending_friends.include?(@friend) #cancel 
     Friendship.breakup(@user, @friend) 
     redirect_to :back 
    elsif @user.friends.include?(@friend) #delete 
     Friendship.breakup(@user, @friend) 
     redirect_to :back 
    end 
    end 

    private 
    def setup_friends 
    @user = User.find(current_user.id) 
    @friend = User.find_by_email(params[:id]) 
    end 
end 

我不確定如何解決這個錯誤,我試圖像下面,但不工作:

<span> 
    <%= form_for friendship, url: friendship_path(requester), method: :put do |f| %> 
    <%= f.hidden_field :user_id, value: current_user.id %> 
    <%= f.hidden_field :friend_id, value: requester.id %> 
    <%= submit_tag "Accept_test", controller: "friendships", action: "update", class: "btn btn_add_friend" %> 
    <% end %> 
</span> 
+2

我不明白你的背後設置了邏輯。你已經在創建方法中創建友誼了嗎?所以,只要接受者接受或拒絕,你只需要更新友好記錄的'接受'(或任何你命名的)值?無需傳遞各種身份證件,只有友情記錄的ID才足以完成所有交易。從我的角度來看,這從Rails的角度來看是不好的做法。 –

回答

2

更新控制器等待id參數,而您傳遞的唯一參數是user_idfriend_id

你可以改變你的路由下面

resources :friendships, only: [:create, :destroy] do 
    collection do 
    put :update 
    end 
end 
相關問題