I'm working on a project in Ruby on Rails (Ruby v.2.2.8, Rails 5.1.4) and have encountered a very strange issue.
For my show method in the controller, I have:
def show
@county = County.find(params[:id])
end
And it works. For update, I have.
def update
@county = County.find(params[:id])
if @county.update(county_params)
redirect_to @county
else
render 'edit'
end
end
In my 'edit', I consistently get an error that @county is nil. The error page indicates that the parameters are being passed as:
{'id'=>4}
as an example. When I use find_by from the rails console, the item is found.
Is there something here I'm missing?
ETA: View Code
<%= form_with model: @county, local: true do |form| %>
<% if @county.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@county.errors.count, "error") %> prohibited
this county from being saved:
</h2>
<ul>
<% @county.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.label :shortname %><br>
<%= form.text_field :shortname %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
ETA Routes for Counties:
counties GET /counties(.:format) counties#index
POST /counties(.:format) counties#create
new_county GET /counties/new(.:format) counties#new
edit_county GET /counties/:id/edit(.:format) counties#edit
county GET /counties/:id(.:format) counties#show
PATCH /counties/:id(.:format) counties#update
PUT /counties/:id(.:format) counties#update
DELETE /counties/:id(.:format) counties#destroy
The error occurs at /counties/:id/edit
@countryinstead of@countyor something?set_countymethod of your county controller?set_countymethod in my controller.