如果单击用户名,则无法显示用户的详细信息
unable to show details of a user if their username is clicked on
我正在制作 Twitter 克隆。在我的推文索引中,我目前在他们的推文旁边有发推文的人的用户名。我想要它,所以当您单击名称时,它会转到他们的个人资料页面。但是,当我单击任何用户名时,它会将您带到当前用户配置文件。有什么办法可以解决这个问题?谢谢
<h1>TWEETS</h1>
<% if user_signed_in? %>
<%= simple_form_for [ @user ,@tweet], id: "form-submit" do |f| %>
<%= f.input :content, label: "Tweet" %>
<%= f.button :submit, class: "btn btn-danger" %>
<% end %>
<% end %>
<br>
<% @tweets.each do |tweet| %>
<ul>
<li>
<%= tweet.created_at.strftime("%B %d %Y, %l:%M%P") %> <br>
<%= tweet.content %>
<%= link_to tweet.user.username, user_path(@user) %>
</li>
</ul>
<% end %>
class TweetsController < ApplicationController
# before_action :authenticate_user!, :except => [:index, :show]
def index
@tweets = Tweet.all.order("created_at DESC")
@tweet = Tweet.new
@user = current_user
end
def show
@tweet = Tweet.find(params[:id])
end
def new
# @tweet = Tweet.new
end
def create
# @user = current_user
@user = User.find(params[:user_id])
@tweet = Tweet.new(tweet_params)
@tweet.user = @user
if @tweet.save
redirect_to user_tweets_path
end
end
def edit
@tweet = Tweet.find(params[:id])
end
def update
@tweet = Tweet.find(params[:id])
@tweet.update(tweet_params)
redirect_to tweets_path
end
private
def tweet_params
params.require(:tweet).permit(:content)
end
end
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
end
<h1>Profile of <%= @user.username %> </h1>
您在列表中引用了错误的用户。
# change this, your @user is defined in controller as current_user
<%= link_to tweet.user.username, user_path(@user) %>
# to
<%= link_to tweet.user.username, user_path(tweet.user) %>
旁注:如果您为 current_user
设置了应用程序级别的辅助方法,则您也不需要设置 @user
我正在制作 Twitter 克隆。在我的推文索引中,我目前在他们的推文旁边有发推文的人的用户名。我想要它,所以当您单击名称时,它会转到他们的个人资料页面。但是,当我单击任何用户名时,它会将您带到当前用户配置文件。有什么办法可以解决这个问题?谢谢
<h1>TWEETS</h1>
<% if user_signed_in? %>
<%= simple_form_for [ @user ,@tweet], id: "form-submit" do |f| %>
<%= f.input :content, label: "Tweet" %>
<%= f.button :submit, class: "btn btn-danger" %>
<% end %>
<% end %>
<br>
<% @tweets.each do |tweet| %>
<ul>
<li>
<%= tweet.created_at.strftime("%B %d %Y, %l:%M%P") %> <br>
<%= tweet.content %>
<%= link_to tweet.user.username, user_path(@user) %>
</li>
</ul>
<% end %>
class TweetsController < ApplicationController
# before_action :authenticate_user!, :except => [:index, :show]
def index
@tweets = Tweet.all.order("created_at DESC")
@tweet = Tweet.new
@user = current_user
end
def show
@tweet = Tweet.find(params[:id])
end
def new
# @tweet = Tweet.new
end
def create
# @user = current_user
@user = User.find(params[:user_id])
@tweet = Tweet.new(tweet_params)
@tweet.user = @user
if @tweet.save
redirect_to user_tweets_path
end
end
def edit
@tweet = Tweet.find(params[:id])
end
def update
@tweet = Tweet.find(params[:id])
@tweet.update(tweet_params)
redirect_to tweets_path
end
private
def tweet_params
params.require(:tweet).permit(:content)
end
end
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
end
<h1>Profile of <%= @user.username %> </h1>
您在列表中引用了错误的用户。
# change this, your @user is defined in controller as current_user
<%= link_to tweet.user.username, user_path(@user) %>
# to
<%= link_to tweet.user.username, user_path(tweet.user) %>
旁注:如果您为 current_user
设置了应用程序级别的辅助方法,则您也不需要设置 @user