简体   繁体   中英

Unsure how create method works - ruby on rails

Ive created a twitter clone. I eventually found a way to get the create method to work. Im not 100% sure why it works though. I understand the if/else statement, its mainly the @tweet.user = @user i dont get. Could someone run through it? Thanks for any help.

 def create @user = current_user @tweet = Tweet.new(tweet_params) @tweet.user = @user authorize @tweet if @tweet.save redirect_to user_tweets_path else redirect_to user_tweets_path end end 

@tweet.user = @user is setting the user on the tweet. It's essentially creating the relationship between @user and @tweet by setting @tweet.user_id to @user.id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM