title: Couldn't find World with 'id'=
body: <p>I have a World parent object and a State child object. I'm trying to create a new State object and rails isn't finding the world id. I'm trying to link to the new state form from the world show page, and the url shows <code>.../worlds/1/states/new</code> so why is this not picking up on the parent id? The error is supposedly coming from this line in the controller <code>@world = World.find(params[:id])</code>. I have tried using <code>(params[:world_id])</code> even.</p>
  
  <p>For brevity I'm only posting the relevant code here. </p>
  
  <p>world.rb</p>
  
  <pre><code>class World &lt; ApplicationRecord
    belongs_to :user
    has_many :states
  end
  </code></pre>
  
  <p>state.rb</p>
  
  <pre><code>class State &lt; ApplicationRecord
    belongs_to :world
    belongs_to :user
  end
  </code></pre>
  
  <p>states_controller.rb</p>
  
  <pre><code>class StatesController &lt; ApplicationController
    before_action :set_state, only: [:show, :edit, :update, :destroy]
    before_action :authenticate_user!, except: [:index, :show]
  
    def index
      @states = State.all
    end
  
    def new
      @world = World.find(params[:id])
      @state = @world.states.build
    end
  
    def create
      @world = World.find(params[:id])
      @state = @world.states.build(state_params)
      @state.user = current_user
  
      respond_to do |format|
        if @state.save
          format.html { redirect_to @state, notice: 'State was successfully created.' }
        else
          format.html { render :new }
        end
      end
    end
  
    private
      def set_state
        @state = State.find(params[:id])
      end
  
      def state_params
        params.require(:state).permit(:name, :summary, :history, :population, :inception, :life_expectancy, :land_mass,
                                      :climate, :industry, :education, :mythology, :law, :culture, :world_id, :user_id)
      end
  end 
  </code></pre>
  
  <p>The link to the new state form in worlds/show.html.erb:</p>
  
  <pre><code>&lt;%= link_to 'New State', new_world_state_path(@world) %&gt;
  </code></pre>
  
  <p>routes.rb</p>
  
  <pre><code>Rails.application.routes.draw do
    resources :states
    resources :worlds
    devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout', sign_up: 'register' }
    root to: "home#index"
  
    resources :users
    resources :worlds do
      resources :states
    end
  end
  </code></pre>
  
  <p>states/_form.html.erb</p>
  
  <pre><code>&lt;div class="form"&gt;
    &lt;%= form_for(state) do |f| %&gt;
      &lt;% if state.errors.any? %&gt;
        &lt;div id="error_explanation"&gt;
          &lt;h2&gt;&lt;%= pluralize(world.errors.count, "error") %&gt; prohibited this state from being saved:&lt;/h2&gt;
  
          &lt;ul&gt;
          &lt;% state.errors.full_messages.each do |message| %&gt;
            &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
          &lt;% end %&gt;
          &lt;/ul&gt;
        &lt;/div&gt;
      &lt;% end %&gt;
  
      &lt;%= f.text_field :name, placeholder: 'Name' %&gt;&lt;br /&gt;
  
      &lt;fieldset&gt;
        &lt;legend&gt;Basic Info&lt;/legend&gt;
          &lt;%= f.text_area :summary, placeholder: 'Summary About', rows: 6 %&gt;&lt;br /&gt;
          &lt;%= f.text_area :history, placeholder: 'History', rows: 6 %&gt;&lt;br /&gt;
          &lt;%= f.text_area :climate, placeholder: 'Climate', rows: 3 %&gt;&lt;br /&gt;
          &lt;%= f.text_area :industry, placeholder: 'Industry', rows: 3 %&gt;&lt;br /&gt;
          &lt;%= f.text_area :education, placeholder: 'Education', rows: 3 %&gt;&lt;br /&gt;
          &lt;%= f.text_area :culture, placeholder: 'Culture', rows: 3 %&gt;&lt;br /&gt;
          &lt;%= f.text_area :law, placeholder: 'Legal System, Crime &amp; Punishment', rows: 3 %&gt;&lt;br /&gt;
          &lt;%= f.text_area :mythology, placeholder: 'Mythology', rows: 3 %&gt;&lt;br /&gt;
      &lt;/fieldset&gt;
  
      &lt;fieldset&gt;
        &lt;legend&gt;Quick Stats&lt;/legend&gt;
          &lt;%= f.text_field :inception, placeholder: 'Inception' %&gt;&lt;br /&gt;
          &lt;%= f.text_field :population, placeholder: 'Population' %&gt;&lt;br /&gt;
          &lt;%= f.text_field :life_expectancy, placeholder: 'Ave. Life Expectance' %&gt;&lt;br /&gt;
          &lt;%= f.text_field :land_mass, placeholder: 'Land Mass' %&gt;&lt;br /&gt;
      &lt;/fieldset&gt;
  
      &lt;p&gt;&lt;%= f.submit %&gt;&lt;/p&gt;
    &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>rails console results when clicking 'New State' link</p>
  
  <pre><code>Started GET "/worlds/1/states/new" for 70.196.17.76 at 2017-05-22 13:43:47 +0000
  Cannot render console from 70.196.17.76! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
  Processing by StatesController#new as HTML
    Parameters: {"world_id"=&gt;"1"}
    User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 1]]
    World Load (0.1ms)  SELECT  "worlds".* FROM "worlds" WHERE "worlds"."id" = ? LIMIT ?  [["id", nil], ["LIMIT", 1]]
  Completed 404 Not Found in 3ms (ActiveRecord: 0.4ms)
  
  
  
  ActiveRecord::RecordNotFound (Couldn't find World with 'id'=):
  
  app/controllers/states_controller.rb:13:in `new'
    Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
    Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
    Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (4.7ms)
    Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
    Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.6ms)
    Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
    Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
    Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (36.6ms)
  </code></pre>