0
votes

I upgraded my rails version from 3.2.13 to 4.0.0 and I also upgraded my gems. When I run application from local server, this is the error I am getting:

Started GET "/signup.html" for 127.0.0.1 at 2016-06-24 11:54:37 +0530

ActionController::RoutingError - undefined local variable or method inherit_resources' for AccountsController:Class: actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:69:inrescue in controller' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:64:in controller' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:44:incall' actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in
call' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in call' meta_request (0.4.0) lib/meta_request/middlewares/app_request_handler.rb:13:incall'
meta_request (0.4.0) lib/meta_request/middlewares/meta_request_handler.rb:13:in call'
warden (1.0.6) lib/warden/manager.rb:35:in
block in call' warden (1.0.6) lib/warden/manager.rb:34:in call' client_side_validations (3.0.12) lib/client_side_validations/middleware.rb:17:incall' rack (1.5.5) lib/rack/etag.rb:23:in call' rack (1.5.5) lib/rack/conditionalget.rb:25:incall' rack (1.5.5) lib/rack/head.rb:11:in call' remotipart (1.2.1) lib/remotipart/middleware.rb:27:incall' actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in
call' rack (1.5.5) lib/rack/session/abstract/id.rb:225:in context' rack (1.5.5) lib/rack/session/abstract/id.rb:220:incall'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in call' activerecord (4.0.0) lib/active_record/query_cache.rb:36:in call' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in call' activerecord (4.0.0) lib/active_record/migration.rb:369:in call' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in
_run__139117187983862327__call__callbacks' activesupport (4.0.0) lib/active_support/callbacks.rb:80:in run_callbacks' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:incall'
rails-dev-tweaks (1.2.0) lib/rails_dev_tweaks/granular_autoload/middleware.rb:36:in call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in
call' airbrake (4.3.7) lib/airbrake/rails/middleware.rb:13:in call' better_errors (2.1.1) lib/better_errors/middleware.rb:84:in protected_app_call' better_errors (2.1.1) lib/better_errors/middleware.rb:79:in better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in
call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in call'
rack-contrib (1.4.0) lib/rack/contrib/response_headers.rb:17:in
call' meta_request (0.4.0) lib/meta_request/middlewares/headers.rb:16:in call' actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:incall'
railties (4.0.0) lib/rails/rack/logger.rb:38:in call_app' railties (4.0.0) lib/rails/rack/logger.rb:21:inblock in call' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in
tagged' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in tagged' railties (4.0.0) lib/rails/rack/logger.rb:21:incall' actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in call' rack (1.5.5) lib/rack/methodoverride.rb:21:incall' rack (1.5.5) lib/rack/runtime.rb:17:in call' activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:incall' rack (1.5.5) lib/rack/lock.rb:17:in call' actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:incall' airbrake (4.3.7) lib/airbrake/user_informer.rb:16:in _call' airbrake (4.3.7) lib/airbrake/user_informer.rb:12:incall' railties (4.0.0) lib/rails/engine.rb:511:in call' railties (4.0.0) lib/rails/application.rb:97:incall' rack (1.5.5) lib/rack/content_length.rb:14:in call' thin (1.5.1) lib/thin/connection.rb:81:inblock in pre_process' thin (1.5.1) lib/thin/connection.rb:79:in pre_process' thin (1.5.1) lib/thin/connection.rb:54:inprocess' thin (1.5.1) lib/thin/connection.rb:39:in receive_data' eventmachine (1.0.9) lib/eventmachine.rb:193:inrun' thin (1.5.1) lib/thin/backends/base.rb:63:in start' thin (1.5.1) lib/thin/server.rb:159:instart' rack (1.5.5) lib/rack/handler/thin.rb:16:in run' rack (1.5.5) lib/rack/server.rb:264:instart' railties (4.0.0) lib/rails/commands/server.rb:84:in start' railties (4.0.0) lib/rails/commands.rb:78:inblock in ' railties (4.0.0) lib/rails/commands.rb:73:in <top (required)>' railties (4.0.0) lib/rails/app_rails_loader.rb:43:inblock in exec_app_rails' railties (4.0.0) lib/rails/app_rails_loader.rb:32:in exec_app_rails' railties (4.0.0) lib/rails/cli.rb:6:in' railties (4.0.0) bin/rails:9:in <top (required)>' () home/user/.rvm/gems/ruby-2.3.0@project/bin/rails:23:in' () home/user/.rvm/gems/ruby-2.3.0@project/bin/ruby_executable_hooks:15:in `'

In my previous version, the routes file is like this:

 match '/signup'   => 'accounts#new', :plan => 'year'

After upgrading, I changed it as:

get '/signup'   => 'accounts#new', :plan => 'year'

"match"is replaced by "get" for all the routes.

This is the accounts_controller where we used inherited_resources.

class AccountsController < ApplicationController

  inherit_resources

  ssl_required :new, :create

  before_filter :authenticate_user!, :except => [:new, :create]

  ...................
end

I couldn't trace why they are used and couldn't find this in Gemfile. Please help me to fix this error.

1

1 Answers

0
votes

inherit_resources was intended to dry up the controller that uses REST conventions. It is a deprecated gem and it no longer maintained in Rails 4. inherit_resources will essentially inherit from InheritedResources::Base which has the REST methods pre-defined.

Instead you can just remove it and define your routes within your controller like you normally would:

class AccountsController < ApplicationController
  ssl_required :new, :create

  before_filter :authenticate_user!, :except => [:new, :create]

  def index
    @accounts = Accounts.all
  end

  def show
    @account = Account.find(params[:id])
  end

  def new
    @account = Account.new
  end

  def create
    @account = Account.new(account_params)
    if @account.save
      redirect_to @account
    else
      render 'new'
    end
  end

  def edit
    @account = Account.find(params[:id])
  end

  def update
    @account = Account.find(params[:id])
    if @account.update_attributes(account_params)
      flash[:notice] = "Successfully updated account!"
      redirect_to @account
    else
      render 'edit'
    end
  end

  def destroy
    @account = Account.find(params[:id])
    @account.destroy
    redirect_to accounts_path
  end

  private

    def account_params
      params.require(:account).permit(:name, :email, etc) # Look up strong params if you are unfamiliar. The name, email, etc. are going to be the only permitted params passed in the methods above as a safety feature so you have to permit the fields being created here.
    end

That is an outline of what inherited resources was adding into the controller with the intent of having "Fat models and skinny controllers". You can find other gems that do similar things, but in my opinion I rather just add the methods myself for readability.

EDIT: It was adding these method definitions, but depending on your app will define what is inside these methods. This would be my best rough guess. Something new in Rails 4 too is the addition of strong params. In the create and update actions you see account_params being called from a private method instead of updating with the raw params inside the methods.