我是个傻瓜。经过对其他问题的详尽搜索,我找不到答案清晰正确的类似帖子。首先,让我解释一下我的应用程序有如下嵌套的关联:
- 用户有一个客户
- 客户有许多帐户
- 用户通过客户拥有许多帐户
到目前为止,我已经用Devise创建了一个用户,然后在控制台中手动将她分配给一个客户对象。还在控制台中创建了相应的Account。(尚未制定嵌套创建的表单)。
我现在想做的就是让用户登录后进入一个"登录"页面,向她显示她的帐户列表。这听起来很简单,但我得到了以下错误:
ActionController::AccountsController#索引中的未知格式
错误引用的代码。。。
@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
我不明白是什么导致了这个错误。这是Accounts索引的视图代码。。。
<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 %>
我承认,这种观点本身可能有其自身的问题。但它甚至还没有进入视野。错误指向控制器,但路由是否有问题?这就是路由.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
似乎应该有一个简单的解决方案,但我是一个noob。有人能帮我理解该怎么办吗?我如何摆脱这个错误,并将我的用户带到她的帐户列表中?
谢谢
我使用的是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)
对于/accounts.ac045b73-8f9c-446f-9cc2-d86b01cea3b7
,您的应用程序采用ac045b73-8f9c-446f-9cc2-d86b01cea3b7
格式,但您的控制器只知道如何处理html
和json
。
您应该验证为什么/users/sign_in
重定向到那个奇怪的链接
我能看到的唯一错误是在你的控制器中,这个:
format.json { render json: @account }
应该是
format.json { render json: @accounts }
请注意@账户上的额外s。但这可能不是全部。如果是,那就太好了!如果不能,你可以发布你的完整帐户控制器和堆栈跟踪,这样我们就可以解决这个问题吗?