Rails/ Devise - How to Create a User and a Post Through the Same form
<p>I've got a Devise model called user.</p>

<p>I've also got a model called projects.</p>

<p>I want the user to create a project when they create their user account. I've got the functionality working; however, the users project isn't saving to the database.</p>

<p>Here's projects_controller.rb</p>

<pre><code>class ProjectsController &lt; ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!
  # GET /projects
  # GET /projects.json
  def index
    @projects = current_user.projects
  end

  # GET /projects/1
  # GET /projects/1.json
  def show
  end

  # GET /projects/new
  def new
    @project = Project.new
  end

  # GET /projects/1/edit
  def edit
  end

  # POST /projects
  # POST /projects.json
  def create
    @project = Project.new(project_params)
    @project.user = current_user
    respond_to do |format|
      if @project.save
        format.html { redirect_to root_path, notice: 'Thanks! Your project has been saved. Please check your email inbox now.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /projects/1
  # PATCH/PUT /projects/1.json
  def update
    respond_to do |format|
      if @project.update(project_params)
        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { render :show, status: :ok, location: @project }
      else
        format.html { render :edit }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    @project.destroy
    respond_to do |format|
      format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_project
      @project = Project.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
      params.require(:project).permit(:service, :location, :details, :email)
    end
end
</code></pre>

<p>and user.rb</p>

<pre><code>class User &lt; ActiveRecord::Base
  has_many :projects, :dependent =&gt; :destroy
  accepts_nested_attributes_for :projects, :reject_if =&gt; lambda { |a| a[:content].blank}, allow_destroy: true
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable
end
</code></pre>

<p>and the Project model</p>

<pre><code>class Project &lt; ActiveRecord::Base
  belongs_to :user
end
</code></pre>

<p>and the form for creating users / their first post</p>

<pre><code>= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |u|

  = devise_error_messages!

  = u.fields_for :project do |p|
    .field = p.text_field :service, placeholder: "What service do you need?"
    .field = p.text_field :location, placeholder: "What's the project location?"


  .field = u.email_field :email, autofocus: true, placeholder: "What's your email address?"

  .actions = u.submit "Click to continue"
</code></pre>

<p>Any ideas as to how I can get my Projects index view to show only the posts that belong to the current user?</p>