2017-05-26 305 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>