title: How to get the id from the URL Rails
body: <p>Inside my application I have three models: user, pub and micropost. </p>
  
  <p>I would like that when I the user creates a micropost on the pub page, a record of the micropost object is saved in the db with the user id and the pub id. 
  To do this in the micropost table I have user_id and pub_id. </p>
  
  <p>The problem is this: the user has to create the micropost when he is on the page showing the pub informations. I would like that after the user presses the button, the page is refreshed and the new micropost is shown. I cannot figure out how to get the id of the pub from the URL and pass it to the create method in the micropost controller, so to create the new micropost with the pub_id also. I can get the user_id from the current_user variable. When the user creates the micropost, the URL changes to <code>/microposts</code> and the id is not found, so maybe my problem is how to save the id from the previus URL so when the user creates the microposts, it is saved with the pub's id.</p>
  
  <p>micropost.rb</p>
  
  <pre><code>class Micropost &lt; ApplicationRecord
    belongs_to :user
    belongs_to :pub 
    default_scope -&gt; { order(created_at: :desc) }
    validates :user_id, presence: true
    validates :content, presence: true, length: { maximum: 240 }
    validates :pub_id, presence: 
  end
  </code></pre>
  
  <p>micropost_controller.rb</p>
  
  <pre><code>class MicropostsController &lt; ApplicationController
      before_action :logged_in_user, only: [:create, :destroy]
  
  def create
      @pub = Pub.find(params[:id]) // @pub gets nil value
      @micropost = current_user.microposts.create!(micropost_params)
      @micropost.pub= @pub // not sure about that
  if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to(:back)
  else
      render 'static_pages/home'
  end
  end
  
  def destroy
  end
  
  private
  
  def micropost_params
    params.require(:micropost).permit(:content, :pub_id)
  end
  
  end
  </code></pre>
  
  <p>_micropost_form.html.erb</p>
  
  <pre><code>&lt;%= form_for(@micropost) do |f| %&gt;
     &lt;%= render 'shared/error_messages', object: f.object %&gt;
     &lt;div class="field"&gt;
       &lt;%= f.text_area :content, placeholder: "Compose new micropost..." %&gt;
     &lt;/div&gt;
     &lt;%= f.submit "Post", class: "btn btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>pub.rb</p>
  
  <pre><code>class Pub &lt; ApplicationRecord
    belongs_to :user
    has_many :microposts, dependent: :destroy
    validates :user_id, presence: true
    validates :address, presence: true, length: {maximum: 140, minimum: 20}
    validates :name, presence: true, length: {maximum: 30, minimum: 5}
  end
  </code></pre>
  
  <p>So my main problem is that I cannot build the new micropost because I cannot have access to the @pub variable from the micropost controller. When the user presses the button, it gets to /microposts route but then I redirect it back so to show me the new micropost on the pub's page. How can I resolve/improve this situation? </p>
  
  <p>Thanks for your help!</p>