0

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

6
  • 1
    Show your view. Is it as simple as a typo? @country instead of @county or something? Commented Oct 27, 2017 at 14:57
  • Can you please share your set_county method of your county controller? Commented Oct 27, 2017 at 15:00
  • Added the view code. I do not have a set_county method in my controller. Commented Oct 27, 2017 at 15:02
  • Can you include the exact error that you're getting, verbatim? Commented Oct 27, 2017 at 15:52
  • Also, what are your routes relating to the Country model? Commented Oct 27, 2017 at 15:55

1 Answer 1

2

How is your edit action in your controller?

You should define @county as well

def edit
  @county = County.find(params[:id])
end
Sign up to request clarification or add additional context in comments.

10 Comments

Not necessary if they are rendering :edit from update action.
Yes, but I understood that the form he showed is in edit action
Yeah, I know, maybe I was unclear. I mean, if he is rendering the edit view from edit action he may need to set the variable in its action
Yes, indeed. Although I did get the impression that it's not the case here. He should reveal edit action to remove ambiguity.
I'm rendering the edit view from the update action - there is no edit action in the controller.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.