Rails 4 - setting associated foreign key value
<p>I have a model called project and another called invite.</p>

<p>The associations are:</p>

<p>Project</p>

<pre><code>has_many :invites
</code></pre>

<p>Invite</p>

<pre><code>belongs_to :project
</code></pre>

<p>I want to allow users who create projects to send invites to other users to join them in working on the project.</p>

<p>I have a form to create invites as follows:</p>

<pre><code>&lt;%= simple_form_for(@invite, :url =&gt; invites_path) do |f| %&gt;
            &lt;%= f.hidden_field :recipient_id, :value =&gt; get_recipient_id %&gt;
            &lt;%#= f.hidden_field :project_id, :value =&gt; current_user.profile.project_id %&gt;
            &lt;%= f.hidden_field :project_id, :value =&gt; @invite.project_id %&gt;
            &lt;%= f.label :email %&gt;
            &lt;%= f.email_field :email %&gt;
            &lt;!-- This field should use :date_picker- but it isnt working --&gt;
            &lt;%= f.input :expiry, :as =&gt; :date, :label =&gt; "When do you need a response to this invitation?"  %&gt;
            &lt;%#= f.input :expiry, :as =&gt; :date_picker, :label =&gt; "When do you need a response to this invitation?"  %&gt;


            &lt;%= f.submit 'Send', :class=&gt;"formsubmit" %&gt;
            &lt;% end %&gt;
</code></pre>

<p>In the above form, I'm trying to set the project_id value.</p>

<p>In the invites controller, I have:</p>

<pre><code>class InvitesController &lt; ApplicationController
    before_action :get_project

    def index
        @invites = @project.invites
    end

    def new
      # @invite = Invite.new
      @invite = @project.invites.build
    end

    def create
      @invite = Invite.new(invite_params)
      @invite.sender_id = current_user.profile.id
      # @project = Project.find(params[:project_id])

      if @invite.save
           #send existing user email invitation to join project team
           InviteMailer.existing_user_invite(@invite).deliver_later 
           format.html { redirect_to @project }

           @invite.recipient.project.push(@invite.project)
        else
            @invite.recipient.project.push(@invite.project)
             InviteMailer.new_user_invite(@invite, new_user_registration_path(:invite_token =&gt; @invite.token)).deliver
        end

         # oh no, creating an new invitation failed

    end

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

    def get_project
      @project = Project.find(params[:project_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def invite_params
      params[:invite].permit(:email, :project_id, :recipient_id)
    end   
end 
</code></pre>

<p>I thought the get_project before action would get the project. Instead, I get an error that says: </p>

<pre><code>undefined method `project_id' for #&lt;Profile:0x007fa179509b60&gt;
Did you mean?  project_ids
               project_ids=
               projects
               object_id
</code></pre>

<p>The error message points to the hidden field in my form for project_id.</p>

<p>Can anyone see where I'm going wrong? </p>