Search code examples
ruby-on-railsruby-on-rails-4activerecordmodel-view-controllernested-resources

Rails 4: ActiveRecord::RecordNotFound in PostsController#edit


This is a basic error and lots of people have ask similar questions, such as:

However, none of them actually helped me solve the issue I am facing now.

In my Rails 4 app, I have four models:

class User < ActiveRecord::Base
  has_many :administrations
  has_many :calendars, through: :administrations
end

class Calendar < ActiveRecord::Base
  has_many :administrations
  has_many :users, through: :administrations
  has_many: :posts
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

class Post < ActiveRecord::Base
  belongs_to :calendar
end

Here are my routes:

Rails.application.routes.draw do

  root to: 'pages#home'

  devise_for :users, :path => 'account'

  resources :calendars do
    resources :posts, shallow: true
  end

end

Here is the problem: when I am on a calendar, for instance http://localhost:3000/calendars/11, I have all the posts that belong to this calendar displayed, with Show, Edit and Destroy links alongside each post.

When I click the Edit link, I am taken to http://localhost:3000/posts/11/edit.5* and I get the following error:

ActiveRecord::RecordNotFound in PostsController#edit
Couldn't find Post with 'id'=11

# Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.

Note: I don't know why I keep getting this weird url, with .5 at the end.

Here is the content of my PostsController:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
    @calendar = Calendar.find(params[:calendar_id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @calendar = Calendar.find(params[:calendar_id])
    @post = @calendar.posts.create(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to calendar_path(@calendar), notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    @calendar = Calendar.find(params[:calendar_id])
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to calendar_path(@calendar), notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @calendar = Calendar.find(params[:calendar_id])
    @post.destroy
    respond_to do |format|
      format.html { redirect_to calendar_path(@calendar), notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:date, :time, :subject, :format, :copy, :media)
    end
end

UPDATE: as per @Nathan's comment, here is the edit.html.erb view:

<h1>Editing Post</h1>

<%= render 'form' %>

<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>

and here is the _form.html.erb partial used in it:

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <p>
      <%= f.label :date %><br>
      <%= f.date_select :date %>
    </p>
    <p>
      <%= f.label :time %><br>
      <%= f.time_select :time %>
    </p>
    <p>
      <%= f.label :subject %><br>
      <%= f.text_field :subject %>
    </p>
    <p>
      <%= f.label :format %><br>
      <%= f.text_field :format %>
    </p>
    <p>
      <%= f.label :copy %><br>
      <%= f.text_area :copy %>
    </p>
    <p>
      <%= f.label :media %><br>
      <%= f.text_field :media %>
    </p>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

UPDATE 2: as per @danielricecodes' answer, here is the result of running rake routes in Terminal:

Prefix Verb   URI Pattern                                 Controller#Action
                   posts GET    /posts(.:format)                            posts#index
                         POST   /posts(.:format)                            posts#create
                new_post GET    /posts/new(.:format)                        posts#new
               edit_post GET    /posts/:id/edit(.:format)                   posts#edit
                    post GET    /posts/:id(.:format)                        posts#show
                         PATCH  /posts/:id(.:format)                        posts#update
                         PUT    /posts/:id(.:format)                        posts#update
                         DELETE /posts/:id(.:format)                        posts#destroy
                    root GET    /                                           pages#home
        new_user_session GET    /account/sign_in(.:format)                  devise/sessions#new
            user_session POST   /account/sign_in(.:format)                  devise/sessions#create
    destroy_user_session DELETE /account/sign_out(.:format)                 devise/sessions#destroy
           user_password POST   /account/password(.:format)                 devise/passwords#create
       new_user_password GET    /account/password/new(.:format)             devise/passwords#new
      edit_user_password GET    /account/password/edit(.:format)            devise/passwords#edit
                         PATCH  /account/password(.:format)                 devise/passwords#update
                         PUT    /account/password(.:format)                 devise/passwords#update
cancel_user_registration GET    /account/cancel(.:format)                   devise/registrations#cancel
       user_registration POST   /account(.:format)                          devise/registrations#create
   new_user_registration GET    /account/sign_up(.:format)                  devise/registrations#new
  edit_user_registration GET    /account/edit(.:format)                     devise/registrations#edit
                         PATCH  /account(.:format)                          devise/registrations#update
                         PUT    /account(.:format)                          devise/registrations#update
                         DELETE /account(.:format)                          devise/registrations#destroy
       user_confirmation POST   /account/confirmation(.:format)             devise/confirmations#create
   new_user_confirmation GET    /account/confirmation/new(.:format)         devise/confirmations#new
                         GET    /account/confirmation(.:format)             devise/confirmations#show
             user_unlock POST   /account/unlock(.:format)                   devise/unlocks#create
         new_user_unlock GET    /account/unlock/new(.:format)               devise/unlocks#new
                         GET    /account/unlock(.:format)                   devise/unlocks#show
          calendar_posts GET    /calendars/:calendar_id/posts(.:format)     posts#index
                         POST   /calendars/:calendar_id/posts(.:format)     posts#create
       new_calendar_post GET    /calendars/:calendar_id/posts/new(.:format) posts#new
                         GET    /posts/:id/edit(.:format)                   posts#edit
                         GET    /posts/:id(.:format)                        posts#show
                         PATCH  /posts/:id(.:format)                        posts#update
                         PUT    /posts/:id(.:format)                        posts#update
                         DELETE /posts/:id(.:format)                        posts#destroy
               calendars GET    /calendars(.:format)                        calendars#index
                         POST   /calendars(.:format)                        calendars#create
            new_calendar GET    /calendars/new(.:format)                    calendars#new
           edit_calendar GET    /calendars/:id/edit(.:format)               calendars#edit
                calendar GET    /calendars/:id(.:format)                    calendars#show
                         PATCH  /calendars/:id(.:format)                    calendars#update
                         PUT    /calendars/:id(.:format)                    calendars#update
                         DELETE /calendars/:id(.:format)                    calendars#destroy

UPDATE 3: as per @Nathan's second comment, here is the content of my show.html.erb calendar view:

<h2><%= @calendar.name %> Calendar</h2>

<h3>Posts</h3>
<% if @calendar.posts.any? %>
    <table>
        <tr>
            <th>Date</th>
            <th>Time</th>
            <th>Subject</th>
            <th>Format</th>
            <th>Copy</th>
            <th>Media</th>
        </tr>
  <% @calendar.posts.each do |post| %>
        <tr>
        <td><%= post.date %></td>
            <td><%= post.time %></td>
            <td><%= post.subject %></td>
            <td><%= post.format %></td>
            <td><%= post.copy %></td>
            <td><%= post.media %></td>
        <td><%= link_to 'View', post %></td>
        <td><%= link_to 'Update', edit_post_path(@calendar, post) %></td>
        <td><%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        </tr>
    </table>
  <% end %>
<% else %>
<p>This calendar does not contain any post yet: just create one with the form below.</p>
<% end %>

<h3>Add a post to <%= @calendar.name %> Calendar:</h3>
<%= form_for([@calendar, @calendar.posts.build]) do |f| %>
  <p>
    <%= f.label :date %><br>
    <%= f.date_select :date %>
  </p>
  <p>
    <%= f.label :time %><br>
    <%= f.time_select :time %>
  </p>
  <p>
    <%= f.label :subject %><br>
    <%= f.text_field :subject %>
  </p>
  <p>
    <%= f.label :format %><br>
    <%= f.text_field :format %>
  </p>
  <p>
    <%= f.label :copy %><br>
    <%= f.text_area :copy %>
  </p>
  <p>
    <%= f.label :media %><br>
    <%= f.text_field :media %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>


<%= link_to 'Edit', edit_calendar_path %> |
<%= link_to 'Back', calendars_path %>

UPDATE 4: here are the routes when I remove the shallow option from the post resource:

Prefix Verb   URI Pattern                                 Controller#Action
                   posts GET    /posts(.:format)                            posts#index
                         POST   /posts(.:format)                            posts#create
                new_post GET    /posts/new(.:format)                        posts#new
               edit_post GET    /posts/:id/edit(.:format)                   posts#edit
                    post GET    /posts/:id(.:format)                        posts#show
                         PATCH  /posts/:id(.:format)                        posts#update
                         PUT    /posts/:id(.:format)                        posts#update
                         DELETE /posts/:id(.:format)                        posts#destroy
                    root GET    /                                           pages#home
        new_user_session GET    /account/sign_in(.:format)                  devise/sessions#new
            user_session POST   /account/sign_in(.:format)                  devise/sessions#create
    destroy_user_session DELETE /account/sign_out(.:format)                 devise/sessions#destroy
           user_password POST   /account/password(.:format)                 devise/passwords#create
       new_user_password GET    /account/password/new(.:format)             devise/passwords#new
      edit_user_password GET    /account/password/edit(.:format)            devise/passwords#edit
                         PATCH  /account/password(.:format)                 devise/passwords#update
                         PUT    /account/password(.:format)                 devise/passwords#update
cancel_user_registration GET    /account/cancel(.:format)                   devise/registrations#cancel
       user_registration POST   /account(.:format)                          devise/registrations#create
   new_user_registration GET    /account/sign_up(.:format)                  devise/registrations#new
  edit_user_registration GET    /account/edit(.:format)                     devise/registrations#edit
                         PATCH  /account(.:format)                          devise/registrations#update
                         PUT    /account(.:format)                          devise/registrations#update
                         DELETE /account(.:format)                          devise/registrations#destroy
       user_confirmation POST   /account/confirmation(.:format)             devise/confirmations#create
   new_user_confirmation GET    /account/confirmation/new(.:format)         devise/confirmations#new
                         GET    /account/confirmation(.:format)             devise/confirmations#show
             user_unlock POST   /account/unlock(.:format)                   devise/unlocks#create
         new_user_unlock GET    /account/unlock/new(.:format)               devise/unlocks#new
                         GET    /account/unlock(.:format)                   devise/unlocks#show
          calendar_posts GET    /calendars/:calendar_id/posts(.:format)     posts#index
                         POST   /calendars/:calendar_id/posts(.:format)     posts#create
       new_calendar_post GET    /calendars/:calendar_id/posts/new(.:format) posts#new
                         GET    /posts/:id/edit(.:format)                   posts#edit
                         GET    /posts/:id(.:format)                        posts#show
                         PATCH  /posts/:id(.:format)                        posts#update
                         PUT    /posts/:id(.:format)                        posts#update
                         DELETE /posts/:id(.:format)                        posts#destroy
               calendars GET    /calendars(.:format)                        calendars#index
                         POST   /calendars(.:format)                        calendars#create
            new_calendar GET    /calendars/new(.:format)                    calendars#new
           edit_calendar GET    /calendars/:id/edit(.:format)               calendars#edit
                calendar GET    /calendars/:id(.:format)                    calendars#show
                         PATCH  /calendars/:id(.:format)                    calendars#update
                         PUT    /calendars/:id(.:format)                    calendars#update
                         DELETE /calendars/:id(.:format)                    calendars#destroy
MacBook-Pro-de-Thibaud:calendy TXC$ rake routes
                  Prefix Verb   URI Pattern                                      Controller#Action
                    root GET    /                                                pages#home
        new_user_session GET    /account/sign_in(.:format)                       devise/sessions#new
            user_session POST   /account/sign_in(.:format)                       devise/sessions#create
    destroy_user_session DELETE /account/sign_out(.:format)                      devise/sessions#destroy
           user_password POST   /account/password(.:format)                      devise/passwords#create
       new_user_password GET    /account/password/new(.:format)                  devise/passwords#new
      edit_user_password GET    /account/password/edit(.:format)                 devise/passwords#edit
                         PATCH  /account/password(.:format)                      devise/passwords#update
                         PUT    /account/password(.:format)                      devise/passwords#update
cancel_user_registration GET    /account/cancel(.:format)                        devise/registrations#cancel
       user_registration POST   /account(.:format)                               devise/registrations#create
   new_user_registration GET    /account/sign_up(.:format)                       devise/registrations#new
  edit_user_registration GET    /account/edit(.:format)                          devise/registrations#edit
                         PATCH  /account(.:format)                               devise/registrations#update
                         PUT    /account(.:format)                               devise/registrations#update
                         DELETE /account(.:format)                               devise/registrations#destroy
       user_confirmation POST   /account/confirmation(.:format)                  devise/confirmations#create
   new_user_confirmation GET    /account/confirmation/new(.:format)              devise/confirmations#new
                         GET    /account/confirmation(.:format)                  devise/confirmations#show
             user_unlock POST   /account/unlock(.:format)                        devise/unlocks#create
         new_user_unlock GET    /account/unlock/new(.:format)                    devise/unlocks#new
                         GET    /account/unlock(.:format)                        devise/unlocks#show
          calendar_posts GET    /calendars/:calendar_id/posts(.:format)          posts#index
                         POST   /calendars/:calendar_id/posts(.:format)          posts#create
       new_calendar_post GET    /calendars/:calendar_id/posts/new(.:format)      posts#new
      edit_calendar_post GET    /calendars/:calendar_id/posts/:id/edit(.:format) posts#edit
           calendar_post GET    /calendars/:calendar_id/posts/:id(.:format)      posts#show
                         PATCH  /calendars/:calendar_id/posts/:id(.:format)      posts#update
                         PUT    /calendars/:calendar_id/posts/:id(.:format)      posts#update
                         DELETE /calendars/:calendar_id/posts/:id(.:format)      posts#destroy
               calendars GET    /calendars(.:format)                             calendars#index
                         POST   /calendars(.:format)                             calendars#create
            new_calendar GET    /calendars/new(.:format)                         calendars#new
           edit_calendar GET    /calendars/:id/edit(.:format)                    calendars#edit
                calendar GET    /calendars/:id(.:format)                         calendars#show
                         PATCH  /calendars/:id(.:format)                         calendars#update
                         PUT    /calendars/:id(.:format)                         calendars#update
                         DELETE /calendars/:id(.:format)                         calendars#destroy

Any idea how I can fix this?


Solution

  • Two arguments are being passed in for a named path (edit_post) which expects one argument. This is causing problems when the controller action set_post tries to determine which :id to use when looking up the record to set as @post.

    It's also why you have that .5 mysteriously appended to the end of your URL — I believe 5 is the :id of post (in that context), and 11 is the :id of @calendar.

    edit_post_path only needs to know the :id of the post you want to edit, so you can fix this by changing <%= link_to 'Update', edit_post_path(@calendar, post) %> to <%= link_to 'Update', edit_post_path(post) %> (note the removal of @calendar).

    If you really do want to edit this post as a resource nested under calendar (at the path /calendars/<calendar_id>/posts/<post_id>/edit), take a look at your config/routes.rb. The shallow: true option (which you used when nesting :posts under :calendars) is what's keeping the nested :edit path from being created.

    You'll need to have that nested :edit path available, and then you can pass two arguments just as you are now (something like edit_calendar_post_path(@calendar, post)).