我花了一段时间,但终于能够通过友谊模式与user_id
和friend_id
相互联系。我遇到的问题是:
点击“添加好友”
<% @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.id), method: :post %>
<% else %>
<%= link_to(
("Unfollow"),
"/friendships/#{ @friendship.id }",
method: :delete ) %>
<% end %>
<% end %>
<% end %>
我得到的回应是:
undefined method `id' for nil:NilClass
Extracted source (around line #86):
<% else %>
<%= link_to(
("Unfollow"),
"/friendships/#{ @friendship.id }",
method: :delete ) %>
<% end %>
<% end %>
如果我遗漏任何代码以便进一步了解可能存在的问题,请告诉我
Rake Routes
friendships POST /friendships(.:format) friendships#create
friendship DELETE /friendships/:id(.:format) friendships#destroy
友情控制器
class FriendshipsController < ApplicationController
def create
# @friendship = current_user.friendships.build
# @friendship.friend_id = params[:friend_id]
# @friendship.user_id = current_user.id
@friendship = current_user.friendships.build(friend_id: params[:friend_id])
if @friendship.save
flash[:notice] = "Added friend."
redirect_to "/users/#{ params[:friend_id] }"
else
flash[:notice] = "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
private
def friendship_params
params.require(:friendship).permit(:user_id, :friend_id)
end
end
用户控制器
def index
@user = User.new
@users = User.all
if current_user
@leaders = @current_user.leaders
end
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
cookies[:user_id] = @user.id
flash[:notice] = "Successfully Registerd"
redirect_to "/"
else
flash[:alert] = @user.errors.full_messages
redirect_to "/"
end
end
def new
@user = User.new
end
def edit
@user = User.friendly.find(params[:id])
current_user
end
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(friend_id: @user.id,
user_id: @current_user.id).first
end
end
def update
@user = User.friendly.find(params[:id])
if @user.update(user_params)
flash[:notice] = "You have successfully update your information"
redirect_to "/"
else
flash[:alert] = @user.errors.full_messages
redirect_to "/"
end
end
def destroy
@user = User.friendly.find(params[:id])
@user.destroy
end
private
def user_params
params.require(:user).permit(:background, :username_or_email, :first_name, :last_name, :email, :password, :user_name, :avatar, :gender, :zip_code, :birthdate)
end
代码位于用户/视图中
<div id="user_profile">
<div id="profile_top">
<a href="/"><p class="profile_logo"></p></a>
<nav>
<div class="profile_loginout">
<%= link_to ("LOGOUT"), "/sessions/new",method: :delete %>
</div>
<div class="profile_user-links">
<a href="/users/<%= current_user.id %>">
<% if current_user.user_name.present? %>
<%= link_to current_user.user_name, user_path(current_user) %>
<% else %>
<%= current_user.first_name %>
<% end %>
</a>
<b class="size">|</b>
<a href="">Settings</a>
<b class="size">|</b>
</div>
</nav>
</div>
<div id="profile_to">
<div class="profile_background_picture">
<%= image_tag @user.background.url(:medium) %>
</div>
<div class="profile_picture">
<%= image_tag @user.avatar.url(:medium) %>
</div>
</div>
</div>
<% if @current_user && @user.id != @current_user.id %>
<% if !@followerlink %>
<form action="/followers" method="POST">
<input type="hidden"
name="authenticity_token"
value="<%= form_authenticity_token %>">
<input type="hidden"
name="leader_id"
value=<%= @user.id %>>
<input type="submit" value="Follow" class="followlink">
</form>
<% else %>
<div class="followlink">
<%= link_to(
("Unfollow"),
"/followers/#{ @followerlink.id }",
method: :delete ) %>
</div>
<% end %>
<% end %>
<p>Username: <%= @user.user_name %></p>
<h2>Friends</h2>
<ul>
<% for friendship in @user.friendships %>
<li>
(<%= link_to "remove", friendship, method: :delete %>)
</li>
<% end %>
</ul>
<p><%= link_to "Find Friends", users_path %></p>
<h2> Users who Have Befriended you </h2>
<ul>
<% for user in @user.inverse_friends %>
<li> <%= h user.user_name %></li>
<% end %>
</ul>
<% @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.id), method: :post %>
<% else %>
<%= link_to "Unfollow", friendships_unfollow_path(@friendship), method: :delete %>
<% end %>
<% end %>
<% end %>
感谢您解决问题的所有帮助或提示。非常感谢。
答案 0 :(得分:1)
在我的头顶上,不确定它是否正在工作但是尝试一下......
在路线中:
delete '/friendships/:id', to: 'friendships_controller#destroy', as: 'friendships_unfollow'
在视图中:
<%= link_to "Unfollow", friendships_unfollow_path(@friendship), method: :delete %>
目前还不清楚你在哪里获得@friendship变量。