0

我正在尝试按照以下链接构建一个友谊系统:如何在 Rails 3 中为社交网络应用程序实现友谊模型?不过有点欠缺。我能够建立关系,但我不确定如何执行以下操作:取消、拒绝、接受。

因此,假设我尝试取消我在未决中执行以下操作的关系,调用我执行以下操作的操作:

<% @customer.pending_friends.each do |pf| %>
  <%= link_to pf.incomplete_name, cancel_friendships_path(:friend_id => pf), :method => :post  %><br />
<% end %>

这里是取消的控制器

  def cancel
    @customer = current_customer
    @friend = Customer.find(params[:friend_id])
    if @customer.pending_friends.include?(@friend)
      Friendship.breakup(@customer, @friend)
      flash[:notice] = "Friendship Canceled"
    else
      flash[:notice] = "No Friendship request"
    end
    redirect_to root_url
  end

这里是我的分手功能

  # Delete a friendship or cancel a pending request.
  def self.breakup(customer, friend)
    transaction do
      destroy(find_by_customer_id_and_friend_id(customer, friend))
      destroy(find_by_customer_id_and_friend_id(friend, customer))
    end
  end

但是,单击取消链接时,我没有收到路由错误。我做错了什么??

在这里请求

路由.rb

resources :friendships do
    collection do
      get 'cancel'
      get 'decline'
    end
  end
  resources :friendships

耙路线

          cancel_friendships GET    /friendships/cancel(.:format)                        friendships#cancel
         decline_friendships GET    /friendships/decline(.:format)                       friendships#decline
                             GET    /friendships(.:format)                               friendships#index
                             POST   /friendships(.:format)                               friendships#create
                             GET    /friendships/new(.:format)                           friendships#new
                             GET    /friendships/:id/edit(.:format)                      friendships#edit
                             GET    /friendships/:id(.:format)                           friendships#show
                             PUT    /friendships/:id(.:format)                           friendships#update
                             DELETE /friendships/:id(.:format)                           friendships#destroy

/********************************************************/


                 friendships GET    /friendships(.:format)                               friendships#index
                             POST   /friendships(.:format)                               friendships#create
              new_friendship GET    /friendships/new(.:format)                           friendships#new
             edit_friendship GET    /friendships/:id/edit(.:format)                      friendships#edit
                  friendship GET    /friendships/:id(.:format)                           friendships#show
                             PUT    /friendships/:id(.:format)                           friendships#update
                             DELETE /friendships/:id(.:format)                           friendships#destroy
4

1 回答 1

1

问题是在您的路线中,您有:

get 'cancel'

但是您的取消链接正在执行发布请求,而不是获取:

<%= link_to ..., ..., :method => :post %>

个人认为应该是删除请求。

在您的路线中:

delete 'cancel'

在您看来:

<%= link_to pf.incomplete_name, cancel_friendships_path(:friend_id => pf), :method => :delete %>

您的代码可能还有其他问题,但这是您必须解决的一件事。

于 2012-09-05T23:31:08.343 回答