Update a nested form
<p>I'm new in RoR, and I'm trying to practice building a web app.
I have a classic app with User who have Post.</p>

<p>An other model Online is used to put the post on a common wall, and it's associated with a nested form Orders which represents pieces available.</p>

<p>So now, I'm trying to update Orders with the action "taked" in my post show view, but rails say that he Couldn't find Post with 'id'= with the private method set online which is working to create orders. (photo)</p>

<p>Error :
<a href="https://i.stack.imgur.com/RVIle.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RVIle.png" alt=""></a>
My code : </p>

<p>Onlines controller : </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>class OnlinesController &lt; ApplicationController
  before_action :authenticate_user!
  before_action :set_post 
  before_action :owned_online, only: [:new, :update]
  before_action :set_online, except: [:taked]
  before_action :set_unline, only: [:taked]


  def new 
    @online = current_user.onlines.build
    @online.post_id = @post.id
    @online.user_id = current_user.id
  end 

  def edit
  end

   def taked 
  @online.orders.update(taked: false, taked_at: Time.zone.now, taked_by: current_user)
end

  def create 
      if Online.where(post_id: params[:post_id]).any?
      @online = Online.where(post_id: params[:post_id]).last.update_attributes(push: false)
      end
     @online = @post.onlines.create(online_params)
    if @online.save
      if @online.portion &lt;= 0
          @online.update(push: false)
          flash[:success] = 'Veuillez indiquer le nombre de parts disponibles '
          redirect_to root_path 
        else
       @online.update(pushed_at: Time.zone.now)
       @online.update(push: true)

       
       flash[:success] = 'Votre post est en ligne !'
      redirect_to root_path
    
    end
    else 
      render 'new'
    end 
  end 




def update  
    if @onlines.update(online_params)
      if @online.push == false
        if @online.portion &lt;= 0
          @online.update(push: false)
          flash[:success] = 'Veuillez indiquer le nombre de parts disponibles '
          redirect_to root_path 
        else
         @online.update(push: true)
         flash[:success] = 'Votre post a bien été pushé !'
         redirect_to root_path      
      end   
    end
    else
      @user.errors.full_messages
      flash[:error] = @user.errors.full_messages
      render :edit
    end
  end


private 

def online_params
  params.require(:online).permit(:user_id, :post_id, :prix, :portion, :push, :pushed_at, orders_attributes: [:id, :taked, :taked_at, :taked_by, :validated_at, :validated_by, :_destroy])
  end 

  def owned_online 
     @post = Post.find(params[:post_id])
  unless current_user == @post.user
    flash[:alert] = "That post doesn't belong to you!"
    redirect_to :back
  end
end  

  def set_post
  @post = Post.find_by(params[:post_id]) 
  end 


  def set_online
    @post = Post.find(params[:post_id])
    @online = Online.find_by(params[:id]) 
  end 

  def set_unline
  @online = Online.find_by(params[:id]) 
end

end</code></pre>
</div>
</div>
</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>class Online &lt; ActiveRecord::Base
  
  belongs_to :post
  belongs_to :user
  has_many :orders
  
  accepts_nested_attributes_for :orders, allow_destroy: true
  
  scope :push, -&gt;{ where(push: true).order("pushed_at DESC") }
end</code></pre>
</div>
</div>
</p>

<p>Views/posts/show :</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code> &lt;div class="btn-group" role="group" aria-label="..."&gt;
  &lt;%= link_to '-  Pusher  - ', new_post_online_path(@post), data: { confirm: 'Confirmer la mise en ligne de #{@title}?' }, class: "btn btn-primary " %&gt;


  &lt;div class="col-md-9"&gt;
    &lt;h3&gt;Parts :&lt;/h3&gt;
    &lt;div id="Orders"&gt;
      
        &lt;ul&gt;
    &lt;%- @post.onlines.each do |online| %&gt;
      &lt;%- online.orders.each do |order| %&gt;
      &lt;%- if order.taked == false %&gt;
      &lt;li&gt;
  &lt;%= link_to 'Take', taked_online_path(online), method: :update, class: "btn btn-warning"%&gt;
      &lt;/li&gt;
      &lt;%end%&gt;
      &lt;%end%&gt;
    &lt;%end%&gt;
  &lt;/ul&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
</div>
</div>
</p>

<p>And the routes :</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>Rails.application.routes.draw do
  get 'profiles/show'

  mount RailsAdmin::Engine =&gt; '/admin', as: 'rails_admin'
  
  devise_for :users, :controllers =&gt; { registrations: 'registrations' }

  resources :posts do 
    resources :comments
    resources :onlines do 
      resources :orders
    end
end

  get ':pseudo', to: 'profiles#show', as: :profile
  get ':pseudo/edit', to: 'profiles#edit', as: :edit_profile
  patch ':pseudo/edit', to: 'profiles#update', as: :update_profile

 put 'online/:id/taked', to: 'onlines#taked', as: :taked_online

  
  

  root 'posts#index'</code></pre>
</div>
</div>
</p>

<p>So if you have any advices for that, I'll take it !!
Thanks</p>