How to create an association between two rails models

后端 未结 5 1131
南笙
南笙 2021-01-16 12:07

This is a newbie question, but I\'m still learning how to create an association between two models in rails. I have a user model and a journal_entry model. The journal entri

5条回答
  •  梦毁少年i
    2021-01-16 12:42

    Ok, so I got this working by adding the user to the create action in my journal_entries_controller.rb. Here's the code I used, but is this the "rails way" to do this?

    def create
      @user = current_user
      @journal_entry = @user.journal_entries.build(params[:journal_entry])
      if @journal_entry.save
        flash[:success] = "Journal entry created!" 
      end
    end
    

提交回复
热议问题