我正在使用Amistad实现友谊模型。我在friendships_controller
:index
,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>
我不知道为什么没有被插入的值时,我按下button.But当我做到这一点通过他们正在插入的控制台手动。
我一定要张贴show.html.erb-的代码用户档案页 – Ashksta
您可以运行'rake routes'并查看生成的网址是什么......这是通过试用学习的最佳方式,而呃ror /混合匹配/播放路线和关键字.. –