Search code examples
ruby-on-railsruby-on-rails-4deviserails-activerecordactioncontroller

Rails 4.1 ActionController::UnknownFormat in index method


I am a noob. After an exhaustive search of other questions, I was unable to find similar posts with clear and correct answers. First, let me explain that my app has nested associations as follows:

  • User has one customer
  • Customer has many accounts
  • User has many accounts through customer

So far, I've created a user with Devise, and then manually assigned her to a customer object in the console. Also created a corresponding Account in the console. (Haven't worked out the form yet for the nested creation).

All I want to do right now is bring the user, once logged in, to a "landing" page which shows her a listing of her accounts. It sounds pretty simple, but I get the following error:

ActionController::UnknownFormat in AccountsController#index

The code referenced by the error...

    @accounts = @user.accounts

          respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @account }
    end

Here is the whole method in the Accounts Controller...

    def index
      @user = current_user
      @accounts = @user.accounts

        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @account }
        end
    end

I don't understand what's causing the error. Here is the view code for the Accounts index...

    <h1>Listing accounts</h1>

    <table id="indexTable" class="table table-striped">
      <thead>
        <tr>
          <th colspan="5"></th>
        </tr>
      </thead>

      <tbody>
         <% if Account.exists? %>
          <% for account in @accounts do %>
          <tr>
            <td><%= account.id %></td>
            <td><%=  %></td>

            <td><%= link_to 'Show', account %></td>
            <td><%= link_to 'Edit', edit_account_path(account) %></td>
            <td><%= link_to 'Destroy', account, method: :delete, data: { confirm: 'Are you sure?' } %></td>
          </tr>
          <% end %>
        <% else %>
          <div class="jumbotron" style="text-align: center; border: 1px solid #DDD; border-radius: 8px;">
          <h1>No accounts to display</h1>
          </div>
        <% end %>
      </tbody>
    </table>

    <br>

    <%= link_to 'New Account', new_account_path %>

I admit, the view itself may have its own issues. But it's not even getting to the view. The error points to the controller, but could it be a problem with routing? This is what routes.rb looks like...

    Rails.application.routes.draw do
      devise_for :users, :controllers => { :registrations => "registrations" }

      devise_scope :user do
        post "/accounts/adminview" => "devise/sessions#new"
      end
    ...
    root 'home#index'
    ...
    resources :accounts do
        member do
          get :adminview
        end
      end

      resources :account_types, :accounts, :administrators, :customers, :transaction_types, :transactions, :users
    ...
    end

It seems like there should be a simple solution for this, but I am a noob. Can anyone help me understand what to do? How do I get rid of this error and take my user to a listing of her accounts?

Thank you

I'm using Rails 4.1.8

UPDATE

As requested, here is the whole stack trace...

    actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:440:in `retrieve_collector_from_mimes'
    actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:256:in `respond_to'
    app/controllers/accounts_controller.rb:11:in `index'
    actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
    actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
    actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
    activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
    activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
    activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
    activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
    activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
    activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
    activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
    activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
    activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
    actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
    actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
    actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
    activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
    activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
    actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
    actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
    activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
    actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
    actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
    actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
    actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
    actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
    actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
    actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
    actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
    actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
    actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
    actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
    warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
    warden (1.2.3) lib/warden/manager.rb:34:in `catch'
    warden (1.2.3) lib/warden/manager.rb:34:in `call'
    rack (1.5.2) lib/rack/etag.rb:23:in `call'
    rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
    rack (1.5.2) lib/rack/head.rb:11:in `call'
    actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
    ctionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
    rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
    rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
    actionpack (4.1.8)         lib/action_dispatch/middleware/cookies.rb:560:in `call'
    activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
    activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
    activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
    actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
    activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
    actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
    actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
    actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
    actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
    actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
    railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
    railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
    activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
    activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
    activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
    railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
    actionpack (4.1.8)         lib/action_dispatch/middleware/request_id.rb:21:in `call'
    rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
    rack (1.5.2) lib/rack/runtime.rb:17:in `call'
    activesupport (4.1.8)         lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
    rack (1.5.2) lib/rack/lock.rb:17:in `call'
    actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
    rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
    railties (4.1.8) lib/rails/engine.rb:514:in `call'
    railties (4.1.8) lib/rails/application.rb:144:in `call'
    rack (1.5.2) lib/rack/content_length.rb:14:in `call'
    puma (2.11.1) lib/puma/server.rb:507:in `handle_request'
    puma (2.11.1) lib/puma/server.rb:375:in `process_client'
    puma (2.11.1) lib/puma/server.rb:262:in `block in run'
    puma (2.11.1) lib/puma/thread_pool.rb:104:in `call'
    puma (2.11.1) lib/puma/thread_pool.rb:104:in `block in spawn_thread'

UPDATE

As requested, the full Accounts controller...

    class AccountsController < ApplicationController
      before_filter :authenticate_user!
      before_action :set_account, only: [:show, :edit, :update, :destroy]

      # GET /accounts
      # GET /accounts.json
      def index
        @user = current_user
        @accounts = @user.accounts

          respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @accounts }
        end
      end

      def adminview
        @accounts = Account.all

        respond_to do |format|
          format.html # adminview.html.erb
          format.json { render json: @accounts }
        end
      end

      # GET /accounts/1
      # GET /accounts/1.json
      def show
      end

      # GET /accounts/new
      def new
        @user = User.find(params[:user_id])
        @customer = @user.customer.build
        @account = @customer.accounts.build

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @account }
        end
      end

      # GET /accounts/1/edit
      def edit
      end

      # POST /accounts
      # POST /accounts.json
      def create
        @account = Account.new(account_params)

        respond_to do |format|
          if @account.save
            format.html { redirect_to @account, notice: 'Account was successfully created.' }
            format.json { render :show, status: :created, location: @account }
          else
            format.html { render :new }
            format.json { render json: @account.errors, status: :unprocessable_entity }
          end
        end
      end

      # PATCH/PUT /accounts/1
      # PATCH/PUT /accounts/1.json
      def update
        respond_to do |format|
          if @account.update(account_params)
            format.html { redirect_to @account, notice: 'Account was successfully updated.' }
            format.json { render :show, status: :ok, location: @account }
          else
            format.html { render :edit }
            format.json { render json: @account.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /accounts/1
      # DELETE /accounts/1.json
      def destroy
        @account.destroy
        respond_to do |format|
          format.html { redirect_to accounts_url, notice: 'Account was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

      private
        # Use callbacks to share common setup or constraints between actions.
        def set_account
          @account = Account.find(params[:id])
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def account_params
          params[:account]
        end
    end

UPDATE

As requested, the logs just before the error (starting at sign in)...

    Started GET "/users/sign_in" for 127.0.0.1 at 2015-03-07 12:28:50 -0500
    Processing by Devise::SessionsController#new as HTML
    Rendered devise/shared/_links.html.erb (0.5ms)
    Rendered devise/sessions/new.html.erb within layouts/application (3.5ms)
    Rendered application/_navigation.html.erb (0.5ms)
    Completed 200 OK in 166ms (Views: 163.0ms | ActiveRecord: 0.0ms)


    Started POST "/users/sign_in" for 127.0.0.1 at 2015-03-07 12:28:54 -0500
    Processing by Devise::SessionsController#create as HTML
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"7Eoj56l0rRcheddj2tSE9sSm+5wXi7/bxVVFsPga+XM=", "user"=>{"login"=>"charlie", "password"=>"[FILTERED]"}, "commit"=>"Log in"}
      User Load (0.9ms)  SELECT  `users`.* FROM `users`  WHERE (lower(username) = 'charlie' OR lower(email) = 'charlie')  ORDER BY `users`.`id` ASC LIMIT 1
    (0.2ms)  BEGIN
    SQL (0.5ms)  UPDATE `users` SET `current_sign_in_at` = '2015-03-07 17:28:54', `last_sign_in_at` = '2015-03-07 17:27:42', `sign_in_count` = 17 WHERE `users`.`id` = x'ac045b738f9c446f9cc2d86b01cea3b7'
    (94.7ms)  COMMIT
    Redirected to http://localhost:3000/accounts.ac045b73-8f9c-446f-9cc2-d86b01cea3b7
    Completed 302 Found in 187ms (ActiveRecord: 96.2ms)


    Started GET "/accounts.ac045b73-8f9c-446f-9cc2-d86b01cea3b7" for 127.0.0.1 at 2015-03-07 12:28:54 -0500
    Processing by AccountsController#index as 
      User Load (0.5ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = x'ac045b738f9c446f9cc2d86b01cea3b7'  ORDER BY `users`.`id` ASC LIMIT 1
    Completed 406 Not Acceptable in 6ms

    ActionController::UnknownFormat (ActionController::UnknownFormat):
      app/controllers/accounts_controller.rb:11:in `index'


    Rendered /home/barnabas/.rvm/gems/ruby-2.1.5/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.9ms)
    Rendered /home/barnabas/.rvm/gems/ruby-2.1.5/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.4ms)
    Rendered /home/barnabas/.rvm/gems/ruby-2.1.5/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
    Rendered /home/barnabas/.rvm/gems/ruby-2.1.5/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (14.3ms)

Solution

  • For /accounts.ac045b73-8f9c-446f-9cc2-d86b01cea3b7 your app is taking as format ac045b73-8f9c-446f-9cc2-d86b01cea3b7 but your controller only knows how to handle html and json.

    You should verify why /users/sign_in is redirecting to that weird link