我正在尝试按照以下链接构建友谊系统:如何在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
但是,点击取消链接时,我收到了无路由错误。我做错了什么?
请求
route.rb
resources :friendships do
collection do
get 'cancel'
get 'decline'
end
end
resources :friendships
rake routes
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
答案 0 :(得分:1)
问题是你的路线中有:
get 'cancel'
但是您的取消链接正在发布帖子请求,而非get:
<%= link_to ..., ..., :method => :post %>
我个人认为它应该是删除请求。
在您的路线中:
delete 'cancel'
在您看来:
<%= link_to pf.incomplete_name, cancel_friendships_path(:friend_id => pf), :method => :delete %>
您的代码可能还有其他问题,但这是您必须解决的一件事。