路由问题&控制器中的动作 - Rails 5

时间:2016-12-07 14:06:58

标签: ruby-on-rails

  

我得到以下与链接接受相关的错误&我的下降   观点 - 这是一个路由问题,但我不确定如何去做。可以   有人建议我如何纠正这个

错误消息

No route matches {:action=>"accept", :controller=>"friendships", :id=>"emma"}

No route matches {:action=>"decline", :controller=>"friendships", :id=>"emma"}
  

视图/用户/ _friends.html.erb

<% @user.requested_friends.each do |requester| %>
  <tr>
    <td><%= link_to(image_tag("img-profile-image-default.png"), requester) %></td> 
    <td><%= link_to requester.firstname, requester %></td>
    <td>
      <%= link_to "Accept",
                  { controller: "friendships", 
                    action: "accept",
                    id: requester.firstname } %>
      <%= link_to "Decline",
                  { controller: "friendships", 
                    action: "decline",
                    id: requester.firstname },
                    confirm: "Really decline friendship with #{requester.firstname}?" %>
    </td> 
  </tr>
<% end %>
  

friendships_controller.rb

class FriendshipsController < ApplicationController
  before_filter :setup_friends

  def create
    Friendship.request(@user, @friend)
    flash[:notice] = "Friend request sent."
    redirect_to :back
  end

  def accept
    if @user.requested_friends.include?(@friend)
      Friendship.accept(@user, @friend)
      flash[:notice] = "Friendship with #{@friend.firstname} accepted!"
    else
      flash[:notice] = "No friendship request from #{@friend.firstname}."
    end
    redirect_to :back
  end

  def decline
    if @user.requested_friends.include?(@friend)
      Friendship.breakup(@user, @friend)
      flash[:notice] = "Friendship with #{@friend.firstname} declined"
    else
      flash[:notice] = "No friendship request from #{@friend.firstname}."
    end
    redirect_to :back
  end

  def delete
    if @user.friends.include?(@friend)
      Friendship.breakup(@user, @friend)
      flash[:notice] = "Friendship with #{@friend.firstname} deleted!"
    else
      flash[:notice] = "You aren't friends with #{@friend.firstname}"
    end
    redirect_to :back
  end

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

路由文件

Rails.application.routes.draw do
  resources :friendships, only: [:create, :update, :destroy]
end


             friendships POST   /friendships(.:format)         friendships#create
              friendship PATCH  /friendships/:id(.:format)     friendships#update
                         PUT    /friendships/:id(.:format)     friendships#update
                         DELETE /friendships/:id(.:format)     friendships#destroy

1 个答案:

答案 0 :(得分:0)

因为您没有任何接受或拒绝的路线。我看到的只有创建,更新,毁灭。你在友谊控制器中这样做,所以在我看来,最好将友谊ID作为主要参数发送。 你可以这样做

resources :friendships, only: [:create, :update, :destroy] do
 member do
  post :accept
  post :decline
 end
end

这将创建路线和路径助手,如

accept_friendship_path(id: friendship.id)
decline_friendship_path(id: friendship.id)

或者您可以使用

accept_friendship_path(friendship)

你仍然可以发送朋友ID作为param,将其添加到链接中,就像这样

link_to "Accept", accept_friendship_path(friendship, friend_id: requester.id)