添加好友时无法关联两个用户
Having trouble associating two users when adding a friend
感谢光临
每当我点击“添加好友”时,我都会得到 "friend added" 但在查看我的控制台时它显示 Friendship id: 10, user_id: 2, friend_id: 0, created_at: "2015-09-11 05:05:27", updated_at: "2015-09-11 05:05:27"> 意思是添加但不关联 friend_id。谁能帮我弄清楚为什么每次我点击“添加朋友”时它都没有保存 friend_id。谢谢大家的帮助。
Schema.rb
create_table "friendships", force: true do |t|
t.integer "user_id"
t.integer "friend_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Routes.rb
root "users#index"
resources :friendships, only: [:create, :destroy]
resources :sessions, only: [:new, :create, :destroy]
resources :users
Users/show
<% @users.each do |user| %>
<% if user.user_name != current_user.user_name %>
<% if @friendshiplink.nil? %>
<%= user.user_name %>
<%= link_to "Add Friend", friendships_path(friend_id: user), method: :post %>
<% else %>
<%= link_to(
("Unfollow"),
"/friendships/#{ @friendshiplink.id }",
method: :delete) %>
<% end %>
<% end %>
<% end %>
用户模型
has_many :friendships
has_many :friends, through: :friendships
has_many :inverse_friendships, class_name: "Friendship", foreign_key: "friend_id"
has_many :inverse_friends, through: :inverse_friendships, source: :user
友情模型
belongs_to :user
belongs_to :friend, class_name: "User"
用户控制器
def show
@users = User.all
@user = User.friendly.find(params[:id])
current_user
if @current_user
@followerlink = Follower.where(leader_id: @user.id,
follower_id: @current_user.id).first
@friendshiplink = Friendship.where(user_id: @user.id,
friend_id: @current_user.id).first
end
end
友情控
def create
@friendship = current_user.friendships.build(friend_id: params[:friend_id])
if @friendship.save
flash[:notice] = "Added friend."
redirect_to root_url
else
flash[:error] = "Unable to add friend."
redirect_to root_url
end
end
def destroy
@friendship = current_user.friendships.find(params[:id])
@friendship.destroy
flash[:notice] = "Removed friendship."
redirect_to current_user
end
def friendship_params
params.require(:friendship).permit(:friend_id, :user_id)
end
再次感谢所有的帮助。如果有任何问题或我遗漏了可能需要的部分代码,请询问。
这可能不是问题,但试一试:
<%= link_to "Add Friend", friendships_path(friend_id: user.id), method: :post %>
或
<%= link_to "Add Friend", { action: :create, controller: 'friendship',:friend_id => user.id }, method:"post" %>
同时为了确保错误,请从您的 console
添加您的 params
更新:以最简单的方式进行
def create
@friendship = current_user.friendships.build
@friendship.friend_id = params[:friend_id]
@friendship.user_id = current_user.id
if @friendship.save
感谢光临
每当我点击“添加好友”时,我都会得到 "friend added" 但在查看我的控制台时它显示 Friendship id: 10, user_id: 2, friend_id: 0, created_at: "2015-09-11 05:05:27", updated_at: "2015-09-11 05:05:27"> 意思是添加但不关联 friend_id。谁能帮我弄清楚为什么每次我点击“添加朋友”时它都没有保存 friend_id。谢谢大家的帮助。
Schema.rb
create_table "friendships", force: true do |t|
t.integer "user_id"
t.integer "friend_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Routes.rb
root "users#index"
resources :friendships, only: [:create, :destroy]
resources :sessions, only: [:new, :create, :destroy]
resources :users
Users/show
<% @users.each do |user| %>
<% if user.user_name != current_user.user_name %>
<% if @friendshiplink.nil? %>
<%= user.user_name %>
<%= link_to "Add Friend", friendships_path(friend_id: user), method: :post %>
<% else %>
<%= link_to(
("Unfollow"),
"/friendships/#{ @friendshiplink.id }",
method: :delete) %>
<% end %>
<% end %>
<% end %>
用户模型
has_many :friendships
has_many :friends, through: :friendships
has_many :inverse_friendships, class_name: "Friendship", foreign_key: "friend_id"
has_many :inverse_friends, through: :inverse_friendships, source: :user
友情模型
belongs_to :user
belongs_to :friend, class_name: "User"
用户控制器
def show
@users = User.all
@user = User.friendly.find(params[:id])
current_user
if @current_user
@followerlink = Follower.where(leader_id: @user.id,
follower_id: @current_user.id).first
@friendshiplink = Friendship.where(user_id: @user.id,
friend_id: @current_user.id).first
end
end
友情控
def create
@friendship = current_user.friendships.build(friend_id: params[:friend_id])
if @friendship.save
flash[:notice] = "Added friend."
redirect_to root_url
else
flash[:error] = "Unable to add friend."
redirect_to root_url
end
end
def destroy
@friendship = current_user.friendships.find(params[:id])
@friendship.destroy
flash[:notice] = "Removed friendship."
redirect_to current_user
end
def friendship_params
params.require(:friendship).permit(:friend_id, :user_id)
end
再次感谢所有的帮助。如果有任何问题或我遗漏了可能需要的部分代码,请询问。
这可能不是问题,但试一试:
<%= link_to "Add Friend", friendships_path(friend_id: user.id), method: :post %>
或
<%= link_to "Add Friend", { action: :create, controller: 'friendship',:friend_id => user.id }, method:"post" %>
同时为了确保错误,请从您的 console
params
更新:以最简单的方式进行
def create
@friendship = current_user.friendships.build
@friendship.friend_id = params[:friend_id]
@friendship.user_id = current_user.id
if @friendship.save