Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3669702
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:16:31+00:00 2026-05-19T02:16:31+00:00

I have a Rails controller where I accidentally defined the ‘edit’ method inside the

  • 0

I have a Rails controller where I accidentally defined the ‘edit’ method inside the ‘create’ method.

My Controller with the error:

class UsersController < ApplicationController
...
  def create
    @user = User.new(params[:user])
    ...

    def edit
      @user = User.find(params[:id])
      @title = "Edit user"
      @check = "BORK" # something I added for testing the rendered output
    end
  end
end

An example test;

it "should have the right title" do
  get :edit, :id => @user
  response.should have_selector('title', :content => 'Edit user')
end

So when I run the tests (I use rspec) and output the response.body, the User edit.html.erb template is rendered correctly; all the instance variables are visible. So all the tests pass.

Visiting the ‘edit’ URL correctly shows an error; the template uses @user instance variable, and it’s not set correctly. Of course correcting the controller fixes the error.

I don’t understand why the tests pass at all and why, in the test, all the instance variable values are visible?

My instinct suggests this is a scope problem? Something about @user being an instance variable, and that in the tests it’s set within the scope of the test, but in my controller it’s within the scope of the inner ‘edit’ method? But how does the test even find the ‘edit’ method? In what scope does that inner ‘edit’ method exist?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:16:31+00:00Added an answer on May 19, 2026 at 2:16 am

    You should realise that the def construct is as much executable code as an if statement. It’s not invalid to put it inside another method, but it won’t be run until the outer method is called:

    >> class Foo
    >>   def foo
    >>     def bar
    >>     end
    >>   end
    >> end
    => nil 
    >> Foo.instance_methods(false)
    => ["foo"] 
    >> Foo.new.foo
    => nil 
    >> Foo.instance_methods(false)
    => ["foo", "bar"] 
    

    The reason this was erroring in your browser was because Rails reloads all (most) of your classes each request. So, even if you had visited the create action – which would cause the edit method to be defined – the following request would have unloaded it again.

    However in the test environment, if an earlier test had called the create action then that would have defined the edit action for future tests. You would see a different result if your tests were run in a different order (which in itself makes it a bad idea to rely on this).

    Generally of course this isn’t what you want at all, so just clear it up and move along 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside ActionController class (rails/actionpack/lib/action_controller.lib) I found several weird code. I don't really have a
I have a Rails application that unfortunately after a request to a controller, has
Using Rails v2.1, lets say you have an action for a controller that is
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a rails controller handling requests to www.mydomain/foo, now I want all traffic
I have the code sequence below in a rails controller action. Before the IF,
I have this command in a Rails controller open(source) { |s| content = s.read
In my Rails controller, I'm creating multiple instances of the same model class. I
I want to make my rails controller more flexible and try to create some
I have a rails controller I dont ever want to set a cookie. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.