我正在制作一个Twitter克隆。在我的tweets索引上,我当前在其tweet旁边有发推文的用户名。我想要它,所以当您单击名称时,它会转到他们的个人资料页面。但是,当我单击任何用户名时,它会将您带到当前用户个人资料。有什么办法解决这个问题?谢谢
<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>
答案 0 :(得分:0)
您在列表中引用了错误的用户。
# 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