Reputation: 1065
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 .../worlds/1/states/new
so why is this not picking up on the parent id? The error is supposedly coming from this line in the controller @world = World.find(params[:id])
. I have tried using (params[:world_id])
even.
For brevity I'm only posting the relevant code here.
world.rb
class World < ApplicationRecord
belongs_to :user
has_many :states
end
state.rb
class State < ApplicationRecord
belongs_to :world
belongs_to :user
end
states_controller.rb
class StatesController < 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
The link to the new state form in worlds/show.html.erb:
<%= link_to 'New State', new_world_state_path(@world) %>
routes.rb
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
states/_form.html.erb
<div class="form">
<%= form_for(state) do |f| %>
<% if state.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(world.errors.count, "error") %> prohibited this state from being saved:</h2>
<ul>
<% state.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.text_field :name, placeholder: 'Name' %><br />
<fieldset>
<legend>Basic Info</legend>
<%= f.text_area :summary, placeholder: 'Summary About', rows: 6 %><br />
<%= f.text_area :history, placeholder: 'History', rows: 6 %><br />
<%= f.text_area :climate, placeholder: 'Climate', rows: 3 %><br />
<%= f.text_area :industry, placeholder: 'Industry', rows: 3 %><br />
<%= f.text_area :education, placeholder: 'Education', rows: 3 %><br />
<%= f.text_area :culture, placeholder: 'Culture', rows: 3 %><br />
<%= f.text_area :law, placeholder: 'Legal System, Crime & Punishment', rows: 3 %><br />
<%= f.text_area :mythology, placeholder: 'Mythology', rows: 3 %><br />
</fieldset>
<fieldset>
<legend>Quick Stats</legend>
<%= f.text_field :inception, placeholder: 'Inception' %><br />
<%= f.text_field :population, placeholder: 'Population' %><br />
<%= f.text_field :life_expectancy, placeholder: 'Ave. Life Expectance' %><br />
<%= f.text_field :land_mass, placeholder: 'Land Mass' %><br />
</fieldset>
<p><%= f.submit %></p>
<% end %>
</div>
rails console results when clicking 'New State' link
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"=>"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)
Upvotes: 1
Views: 89
Reputation: 33420
Modify your link_to
helper to specify and tell to Rails what's the parameter you're sending through it:
From:
<%= link_to 'New State', new_world_state_path(@world) %>
To:
<%= link_to 'New State', new_world_state_path(id: @world) %>
id
because you're trying to find the World
through :id
as param.
Try also changing the param
that's being received within the controller where you're setting the @world
variable:
def new
@world = World.find(params[:world_id])
...
end
In the show.html.erb
:
<%= link_to 'New World', new_world_state_path(world_id: @world) %>
Update: What we made:
In the app/views/worlds/show.html.erb
to change the way the param was being setted:
From:
<%= link_to 'New Nation', new_world_state_path(world_id: @world_id) %> # @world_id wasn't defined
To:
<%= link_to 'New Nation', new_world_state_path(world_id: @world.id) %>
In the /app/views/states/_form.html.erb
to add the world_id
as a hidden_field
:
<%= f.hidden_field :world_id, value: @world.id %>
And then in app/controllers/states_controller.rb
to change the way the params were being received:
def new
@world = World.find(params[:world_id])
@state = @world.states.build
end
def create
@world = World.find(params[:state][:world_id])
...
Upvotes: 2
Reputation: 1963
The world_id while it is passed to the :new action, it may not be passed back on the form to the create action.
Your state_params are expecting a :world_id to be sent back so add a hidden field to send it back on the form.
new.html.erb
<%= f.hidden_field :world_id, :value => @world.id %>
and update the create action to
@world = World.find(params[:world_id])
Upvotes: 2