Rails form_for results in POST instead of PUT when trying to edit
<p>I am using <strong>Rails 4</strong> and have the following error.</p>

<blockquote>
  <p>Routing Error<br>
  No route matches [POST] "/logs/1/meals/13/edit</p>
</blockquote>

<p>I’m passing form_for the model object using :meal and the edit page is rendering correctly. However, Rails does not seem to be checking whether or not the meal object has already been saved, so it keeps trying to send the form to the #create action and tries make a POST request instead of sending the form to the update action and making a PUT request when I hit submit. </p>

<p><strong>How do I get the form_for to recognize that I am trying to update an existing object and that PUT is needed instead of POST?</strong> Everything else is working and I’ve run all of my migrations. I’m pretty new to Rails, and I’ve spent almost all day trying to figure this out on my own. Please help!  </p>

<p>And just to note, when I tried to pass in the model object as @meal.log instead of :meal, Rails was no longer able to recognize :calorie_estimate or :meal_description. Passing the model object as @meal.log left me with a no method error.</p>

<p>meals/edit.html.erb</p>

<pre><code>&lt;h3&gt; EDIT MEAL &lt;/h3&gt;
&lt;%= form_for(:meal) do |f| %&gt;
&lt;div id="meal-form"&gt;
    &lt;%= f.text_field :calorie_estimate, class: 'meal-form-fields', :placeholder =&gt; "Calorie Estimate" %&gt;
    &lt;%= f.text_field :meal_description, class: 'meal-form-fields', :placeholder =&gt; "Food Description" %&gt;
  &lt;div class="submit-form" style="width: 75px; height: 15px;"&gt;
    &lt;%= f.submit 'UPDATE', :class =&gt; 'submit-form-text' %&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;% end %&gt;
</code></pre>

<p>meals_controller.rb</p>

<pre><code>class MealsController &lt; ApplicationController
  include MealsHelper

  def create
    @meal = Meal.new(meal_params)
    @meal.log_id = params[:log_id]
    @meal.save

    redirect_to log_path(@meal.log)
  end

  def edit
    @meal = Meal.find(params[:id])
  end

  def update
    @meal = Meal.find(params[:id])
    @meal.update(meal_params)
    redirect_to log_path(@log)
  end

  def meal_params
    params.require(:meal).permit(:calorie_estimate, :meal_description)
  end

end
</code></pre>

<p>possible routes:</p>

<pre><code>Prefix Verb   URI Pattern                            Controller#Action
         root GET    /                                      logs#index
    log_meals GET    /logs/:log_id/meals(.:format)          meals#index
              POST   /logs/:log_id/meals(.:format)          meals#create
 new_log_meal GET    /logs/:log_id/meals/new(.:format)      meals#new
edit_log_meal GET    /logs/:log_id/meals/:id/edit(.:format) meals#edit
     log_meal GET    /logs/:log_id/meals/:id(.:format)      meals#show
              PATCH  /logs/:log_id/meals/:id(.:format)      meals#update
              PUT    /logs/:log_id/meals/:id(.:format)      meals#update
              DELETE /logs/:log_id/meals/:id(.:format)      meals#destroy
         logs GET    /logs(.:format)                        logs#index
              POST   /logs(.:format)                        logs#create
      new_log GET    /logs/new(.:format)                    logs#new
     edit_log GET    /logs/:id/edit(.:format)               logs#edit
          log GET    /logs/:id(.:format)                    logs#show
              PATCH  /logs/:id(.:format)                    logs#update
              PUT    /logs/:id(.:format)                    logs#update
              DELETE /logs/:id(.:format)                    logs#destroy
</code></pre>

<p>routes.rb</p>

<pre><code>Rails.application.routes.draw do

  root to: 'logs#index'

  resources :logs do
    resources :meals
  end
end
</code></pre>

<p>schema.rb</p>

<pre><code>ActiveRecord::Schema.define(version: 20160128205351) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "logs", force: :cascade do |t|
    t.string   "entry_date"
    t.integer  "calorie_goal"
    t.string   "notes"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "meals", force: :cascade do |t|
    t.integer  "calorie_estimate"
    t.string   "meal_description"
    t.integer  "log_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end
</code></pre>