title: Ruby on Rails: finding a model instance from a different controller
body: <p>Currently I have a has_many and belongs_to association between a Project model and a Invitation model. I have a form_form invitation in the show action of the projects controller.</p>
  
  <p>projects/show.html.erb</p>
  
  <pre><code>&lt;div class="center"&gt;
      &lt;h1&gt;&lt;%= @project.title %&gt;&lt;/h1&gt;
  
      &lt;%= form_for @invitation do |f| %&gt;
          &lt;%= f.collection_select :user_id, User.all, :id, :first_name %&gt;&lt;br&gt;
          &lt;%= f.submit "Send Invitation", class: "btn btn-primary" %&gt;
      &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Choosing a target user and submitting the form will take the current user to the 'new' invitation page, and the user_id will have been saved for the new invitation. However, I also need a project_id to be saved, and I cannot figure out how I could do this. I was trying to see if I can define an instance variable @project in the 'create' action of the invitations_controller but I can't find out how. </p>
  
  <p>invitations_controller.rb</p>
  
  <pre><code>class InvitationsController &lt; ApplicationController
  def new
      @invitation = Invitation.new
  end
  
  def create
      @invitation = @project.create_invitation(invitation_params)
      if @invitation.save
          flash[:success] = "Invitation sent!"
          redirect_to @invitation
      else
          render 'new'
      end
  end
  
  def show
      @invitation = Invitation.find(params[:id])
  end
  
  private
      def invitation_params
          params.require(:invitation).permit(:user_id, :project_id, :description)
      end
  end
  </code></pre>
  
  <p>Please help me out. Thanks!</p>