2017-05-26 306 views
1

我正在使用Amistad實現友誼模型。我在friendships_controllerindex,request_friend,approve_friend,remove_friend, block_friend中有五個動作。Ruby on Rails路由錯誤

我不確定如何在路徑文件中定義路由,我嘗試使用resources :friendships,但無法通過用戶配置文件發出好友請求。

以下是文件:

的routes.rb:

resources :friendships 

friendships_controller.rb:

class FriendshipsController < ApplicationController 
    before_filter :authenticate_user! 

    def index 
     @friends = current_user.friends 
     @pending_invited_by = current_user.pending_invited_by 
     @pending_invited = current_user.pending_invited 
    end 

    def request_friend 
     @friend = User.find(params[:id]) 
     #if [email protected] 
     # redirect_to :back 
     #end 
     current_user.invite @friend 
     redirect_to :back 
    end 

    def approve_friend 
     @friend = User.find(params[:id]) 
     current_user.approve @friend 
     redirect_to :back 
    end 

    def remove_friend 
     @friend = User.find(params[:id]) 
     current_user.remove_friendship @friend 
     redirect_to :back 
    end 

    def block_friend 
     @blocking = User.find(params[:id]) 
     current_user.block @blocking 
     redirect_to :back 
    end 
end 

我想用戶A可以發送用戶B的朋友請求用戶B接受它,刪除它或忽略它。

這裏是我的show.html.erb

<span> 
    <% if @user == current_user %> 
    <%= link_to 'Edit Profile', edit_profile_path(@user.user_name),class: 'btn edit-button' %> 
    <% elsif current_user.friend_with? @user %>Already Friends! 
    <%= button_to "Delete!",friendship_path(id: @user.id, friendship_action: 'remove_friendship'), method: :delete, class: "btn btn-primary" %> 
    <% elsif current_user.invited? @user %>Friend request sent! 
    <%= button_to "Unsend!", friendship_path(id: @user.id), :method => :delete, class: "btn btn-small btn-primary" %> 
    <% elsif current_user.invited_by? @user %> 
    <%= button_to "Accept!", friendship_path(@user.id, friendship_action: 'approve'), :method => :patch, class: "btn btn-small btn-primary" %> 
    <%= button_to "Reject!", friendship_path(@user.id, friendship_action: 'remove_friendship'), :method => :delete, class: "btn btn-small btn-primary" %> 
    <% else %> 
    <%= form_tag friendship_path(@user.id,friendship_action: 'invite') do %> 
     <%= submit_tag "Request Friend", class: "btn btn-primary" %> 
    <% end %> 
    <% end %> 
</span> 

This is the process going on in console when i manually do the friendships-i.e. user1.invite user2-it is working and changes are being made in the friendhips table-i used sqlitebrowser to verify

Here is what is going on when i press the link_to in my show.html.erb.Ive only included parts of the console related to the friendship model

我不知道爲什麼沒有被插入的值時,我按下button.But當我做到這一點通過他們正在插入的控制檯手動。

+0

我一定要張貼show.html.erb-的代碼用戶檔案頁 – Ashksta

+0

您可以運行'rake routes'並查看生成的網址是什麼......這是通過試用學習的最佳方式,而呃ror /混合匹配/播放路線和關鍵字.. –

回答

0

如果我理解正確的,這是你的routes.rb應該怎麼看起來像

你可以傳遞一個塊的資源方法來定義嵌套的資源。

resources :friendships do 
    member do 
    post 'approve_friendship', to: 'friendships#approve_friendship' 
    post 'request_friendship', to: 'friendships#request_friendship' 
    delete 'remove_friendship', to: 'friendships#remove_friendship' 
    post 'block_friendship', to: 'friendships#block_friendship' 
    end 
end 

這將定義端點爲POST /friendships/:id/approve_friendship

您應該檢查導軌文檔的詳細信息:http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

0

爲什麼不直接使用標準的動詞?

class FriendshipsController < ApplicationController 

    before_filter :authenticate_user! 

    def index 
    @friends = current_user.friends 
    @pending_invited_by = current_user.pending_invited_by 
    @pending_invited = current_user.pending_invited 
    end 

    def new 
    @friend = User.find(params[:id]) 
    current_user.invite @friend 
    redirect_to :back 
    end 

    def create 
    @friend = User.find(params[:id]) 
    current_user.approve @friend 
    redirect_to :back 
    end 

    def update 
    @friend = User.find(params[:id]) 
    current_user.send blocking_method, @friend 
    redirect_to :back 
    end 

    def destroy 
    @friend = User.find(params[:id]) 
    current_user.remove_friendship @friend 
    redirect_to :back 
    end 

private 

    def blocking_method 
    current_user.blocking?(@friend) ? :unblock : :block 
    end 

end 

然後,你可以這樣做:

resources :friendships 

就像你開始使用,而不必猴子周圍的路線。

此外,在update方法(又名'blocking')中,我添加了一個切換,以便您可以允許用戶阻止和取消阻止朋友。

現在,當您查看您的方法時,除了在current_user上進行的呼叫之外,它們基本相同。那麼爲什麼不細細的東西呢?

class FriendshipsController < ApplicationController 
    ALLOWED_FRIENDSHIP_ACTIONS = %w('invite' 'approve' 'remove_friendship' 'block', 'unblock') 

    before_filter :authenticate_user! 

    def index 
    @friends = current_user.friends 
    @pending_invited_by = current_user.pending_invited_by 
    @pending_invited = current_user.pending_invited 
    end 

    def update 
    @friend = User.find(params[:id]) 
    current_user.send(friendship_action, @friend) if friendship_action 
    redirect_to :back 
    end 

private 

    def friendship_action 
    if fa = params[:friendship_action] 
     return fa if ALLOWED_FRIENDSHIP_ACTIONS.include?(fa) 
    end 
    end 

end 

然後你可以這樣做:

resources :friendships, :only => [:index, :update] 

而且使用它是這樣的:

link_to friendship_path(id: @friend.id, friendship_action: 'invite'), method: :patch 

你必須猴子與@friend.id位有點取決於你在哪裏上生成你的link_to(或你的form,這取決於你如何做)。

我不知道。對我來說,這似乎是更乾淨的代碼。更少的線條,更少的重複,更緊密地遵循慣例。但是,無論什麼浮動你的小船。

迴應置評

嘗試下面的變化...

此:

<%= button_to "Delete!",friendship_path(id: @user.id, friendship_action: 'remove_friendship'), method: :delete, class: "btn btn-primary" %> 

應該是這樣的:

<%= button_to "Delete!", friendship_path(id: @user.id, friendship_action: 'remove_friendship'), method: :patch, class: "btn btn-primary" %> 

爲什麼?因爲您再也沒有delete方法,請記住?只有indexpatch

更改此:

<%= button_to "Unsend!", friendship_path(id: @user.id), :method => :delete, class: "btn btn-small btn-primary" %> 

要這樣:

<%= button_to "Unsend!", friendship_path(id: @user.id, friendship_action: 'unsend'), method: :patch, class: "btn btn-small btn-primary" %> 

您需要助手裏面的friendship_action。並且,:delete變成:patch

此:

<%= button_to "Accept!", friendship_path(@user.id, friendship_action: 'approve'), :method => :patch, class: "btn btn-small btn-primary" %> 

我將改變爲:

<%= button_to "Accept!", friendship_path(id: @user.id, friendship_action: 'approve'), method: :patch, class: "btn btn-small btn-primary" %> 

@user.id前面添加id:可能的事情。在這裏,你得到了:method,但爲了保持一致,我將重新格式化爲method: :patch

這裏:

<%= button_to "Reject!", friendship_path(@user.id, friendship_action: 'remove_friendship'), :method => :delete, class: "btn btn-small btn-primary" %> 

更改爲:

<%= button_to "Reject!", friendship_path(id: @user.id, friendship_action: 'remove_friendship'), method: :patch, class: "btn btn-small btn-primary" %> 

添加id:delete:patch

最後,這樣的:

<%= form_tag friendship_path(@user.id,friendship_action: 'invite') do %> 
     <%= submit_tag "Request Friend", class: "btn btn-primary" %> 
    <% end %> 

要這樣:

<%= button_to "Request Friend", friendship_path(id: @user.id, friendship_action: 'invite'), method: :patch, class: "btn btn-primary" %> 

您使用button_to其他任何地方。爲什麼不在這裏?

這是一起:

<span> 
    <% if @user == current_user %> 
    <%= link_to 'Edit Profile', edit_profile_path(@user.user_name),class: 'btn edit-button' %> 
    <% elsif current_user.friend_with? @user %>Already Friends! 
    <%= button_to "Delete!", friendship_path(id: @user.id, friendship_action: 'remove_friendship'), method: :patch, class: "btn btn-primary" %> 
    <% elsif current_user.invited? @user %>Friend request sent! 
    <%= button_to "Unsend!", friendship_path(id: @user.id, friendship_action: 'unsend'), method: :patch, class: "btn btn-small btn-primary" %> 
    <% elsif current_user.invited_by? @user %> 
    <%= button_to "Accept!", friendship_path(id: @user.id, friendship_action: 'approve'), method: :patch, class: "btn btn-small btn-primary" %> 
    <%= button_to "Reject!", friendship_path(id: @user.id, friendship_action: 'remove_friendship'), method: :patch, class: "btn btn-small btn-primary" %> 
    <% else %> 
    <%= button_to "Request Friend", friendship_path(id: @user.id, friendship_action: 'invite'), method: :patch, class: "btn btn-primary" %> 
</span> 
+0

我收到此錯誤,當我點擊拒絕按鈕:沒有路線匹配[刪除]「/友誼/ 2」也沒有他們工作 - 接受,拒絕,請求朋友,取消發送,刪除朋友。 @jvillian我使用了縮短的控制器和你提到的路線。 – Ashksta

+0

我已經在上面添加了show.html.erb文件。對不起,它缺少indentation.i無法弄清楚如何添加縮進。 – Ashksta

+0

嗨@jvillian我已經做了一切它仍然沒有工作,當我按下按鈕沒有任何反應。我已經檢查控制檯一些變化正在進行,但友誼表沒有被修改,以反映變化。我能夠使用u1.invite u2,u2.approve u1等 – Ashksta

0

我終於得到了它working.There是使它更清潔,減少無行,避免重複,也讓它緊跟約定範圍。

friendships_controller.rb

class FriendshipsController < ApplicationController 
    before_filter :authenticate_user! 

    def index 
     @friends = current_user.friends 
     @pending_invited_by = current_user.pending_invited_by 
     @pending_invited = current_user.pending_invited 
    end 

    def request_friend 
     @friend = User.find(params[:id]) 
     #if [email protected] 
     # redirect_to :back 
     #end 
     current_user.invite @friend 
     redirect_to :back 
    end 

    def approve_friend 
     @friend = User.find(params[:id]) 
     current_user.approve @friend 
     redirect_to :back 
    end 

    def remove_friend 
     @friend = User.find(params[:id]) 
     current_user.remove_friendship @friend 
     redirect_to :back 
    end 

    def block_friend 
     @blocking = User.find(params[:id]) 
     current_user.block @blocking 
     redirect_to :back 
    end 
end 

的routes.rb

resources :friendships do 
    member do 
     post 'approve_friend', to: 'friendships#approve_friend' 
     post 'request_friend', to: 'friendships#request_friend' 
     delete 'remove_friend', to: 'friendships#remove_friend' 
     post 'block_friend', to: 'friendships#block_friendship' 
    end 
end 

show.html.erb

<span> 
      <% if @user == current_user %> 
      <%= button_to 'Edit Profile', edit_profile_path(@user.user_name),class: 'btn edit-button' %> 
      <% elsif current_user.friend_with? @user %>Already Friends! 
      <%= button_to "Delete!", remove_friend_friendship_path(id: @user.id), method: :delete, class: "btn btn-primary" %> 
      <% elsif current_user.invited? @user %>Friend request sent! 
      <%= button_to "Unsend!", remove_friend_friendship_path(id: @user.id), method: :delete, class: "btn btn-small btn-primary" %> 
      <% elsif current_user.invited_by? @user %> 
      <%= button_to "Accept!", approve_friend_friendship_path(id: @user.id), method: :post, class: "btn btn-small btn-primary" %> 
      <%= button_to "Reject!", remove_friend_friendship_path(id: @user.id), method: :delete, class: "btn btn-small btn-primary" %> 
      <% else %> 
      <%= button_to "Request Friend", request_friend_friendship_path(id: @user.id),method: :post,class: "btn btn-primary" %> 
      <% end %> 
</span>