Rails 4.1 ActionController::UnknownFormat 索引方法

Rails 4.1 ActionController::UnknownFormat in index method

我是菜鸟。在对其他问题进行详尽搜索后,我无法找到具有明确和正确答案的类似帖子。 首先,让我解释一下我的应用程序具有如下嵌套关联:

到目前为止,我已经使用 Devise 创建了一个用户,然后在控制台中手动将她分配给一个客户对象。在控制台也创建了对应的Account。 (嵌套创建的形式还没想好)

我现在想做的就是让用户在登录后进入 "landing" 页面,该页面显示她的帐户列表。听起来很简单,但我收到以下错误:

AccountsController 中的 ActionController::UnknownFormat#index

错误引用的代码...

    @accounts = @user.accounts

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

这是帐户控制器中的整个方法...

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

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

我不明白是什么导致了错误。这是帐户索引的查看代码...

    <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 %>

我承认,观点本身可能有其自身的问题。但它甚至没有进入视野。错误指向controller,但会不会是routing的问题?这就是 routes.rb 的样子...

    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

似乎应该有一个简单的解决方案,但我是一个菜鸟。谁能帮我理解该怎么做?如何消除此错误并将我的用户带到她的帐户列表?

谢谢

我正在使用 Rails 4.1.8

更新

根据要求,这是整个堆栈跟踪...

    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'

更新

根据要求,完整的帐户控制器...

    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

更新

根据要求,错误之前的日志(从登录开始)...

    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)

我能看到的唯一错误是在你的控制器中,这个:

format.json { render json: @account }

应该是

format.json { render json: @accounts }

注意@accounts 上的额外 's'。但这可能不是全部。如果是,太好了!如果不能,你可以 post 你的完整帐户控制器和堆栈跟踪,以便我们可以解决这个问题吗?

对于 /accounts.ac045b73-8f9c-446f-9cc2-d86b01cea3b7,您的应用采用格式 ac045b73-8f9c-446f-9cc2-d86b01cea3b7,但您的控制器只知道如何处理 htmljson

你应该验证为什么 /users/sign_in 重定向到那个奇怪的 link