I'm trying to create a twitter clone. I'm at the point where a user can post a tweet and it shows the content and the time it was posted. However i want it so the username is also next to the tweet of whoever tweeted it. i'm unsure how to do this as the error is currently 'Couldn't find User without an ID' in my tweet controller create method. I'm also not sure of the syntax to display the username in index.html.erb. thanks.
class TweetsController < ApplicationController
def index
@tweets = Tweet.all.order("created_at DESC")
@tweet = Tweet.new
# @user = User.find(params[:id])
end
def show
@tweet = Tweet.find(params[:id])
end
def new
# @tweet = Tweet.new
end
def create
@user = User.find(params[:id])
@tweet = Tweet.new(tweet_params)
@tweet.user = @user
if @tweet.save
redirect_to 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(:user_id,:content)
end
end
<h1>TWEETS</h1>
<%# @users.each do |user| %>
<%#= user.username %>
<%# end %>
<%= simple_form_for @tweet, id: "form-submit" do |f| %>
<%= f.input :content, label: "Tweet" %>
<%= f.button :submit, class: "btn btn-danger" %>
<% end %>
<br>
<% @tweets.each do |tweet| %>
<ul>
<li>
<%= tweet.created_at.strftime("%B %d %Y, %l:%M%P") %> <br>
<%= tweet.content %>
<%#= tweet.user.username %>
<%#= tweet.user.username %>
</li>
</ul>
<% end %>
Aucun commentaire:
Enregistrer un commentaire