我試圖呈現以下部分:軌道4個content_for和產量顯示空白頁
<% content_for :admin_content do %>
<h3>Listing All Accounts</h3>
<%= paginate (@accounts) %>
<table id="indexTable" class="table table-striped">
...
</table>
<%= paginate (@accounts) %>
<br>
<%= link_to 'New Account', new_account_path %>
<% end %>
(有其他類似的諧音以及)
...下面的佈局:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<%= render 'navigation' %>
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
<main>
<%= render partial: 'admin_navigation' %>
<%= yield :admin_content %>
</main>
<footer>
...
</footer>
</body>
</html>
我試圖使用content_for的原因是,我有類似上面的部分,我想渲染時的鏈接我一對夫婦的其他網頁Š點擊導航:
<div id="panel">
<div class="navbar subnav navbar-inverse admin-nav" role="navigation">
<div class="navbar-inner">
<div class="container-fluid">
<h2>Admin Dashboard</h2>
<ul class="pager subnav-pager">
<div class="btn-group btn-group-justified" role="group" aria-label="navigation">
<span role=button><%= link_to "Manage Accounts", {:action=>"manage_accounts"}, :class => "btn btn-primary" %></span>
<span role=button><%= link_to "Manage Customers", {:action=>"manage_customers"}, :class => "btn btn-primary" %></span>
<span role=button><%= link_to "Manage Transactions", {:action=>"manage_acct_transactions"}, :class => "btn btn-primary" %></span>
</div>
</ul>
</div>
</div>
</nav>
</div>
上面的鏈接調用控制器各自的方法,這些方法如下:
def manage_accounts
@accounts = Account.order('id').page(params[:page]).per(15)
render partial: 'manage_accounts'
end
def manage_customers
@customers = Customer.order('lastname').page(params[:page]).per(15)
render partial: 'manage_customers'
end
def manage_acct_transactions
@acct_transactions = AcctTransaction.order('date DESC').page(params[:page]).per(15)
render partial: 'manage_acct_transactions'
end
但是,當您單擊導航鏈接,它只是顯示一個空白頁根本沒有html。控制檯表示按照控制器加載對象,並且正在渲染部分。 但它在哪裏?
我試過各種各樣的方式來得到這個工作。我得到的最接近的實際上是而沒有 content_for/yield,並且只是調用方法,這當然使得局部就像它自己的頁面一樣(壞)。曾經有一種叫做replace_html的東西可能適用於我正在嘗試做的事情,但我正在使用Rails 4.1.8。
最初,我用AJAX設置了這個導航,但它在這裏不起作用。這些部分具有CRUD操作以及重定向的鏈接。使用AJAX導致了各種CSRF錯誤,並且似乎爲應該非常簡單的事情增添了不必要的複雜層次。
爲什麼上面的代碼沒有渲染任何東西?
我採取了錯誤的做法嗎?
感謝
編輯:添加日誌頁面請求..
Started GET "/administrators/145a435c-6632-4d54-aca3-5e834b9e2d41/adminview" for 127.0.0.1 at 2015-04-01 07:58:41 -0400
Processing by AdministratorsController#adminview as HTML
Parameters: {"id"=>"145a435c-6632-4d54-aca3-5e834b9e2d41"}
[1m[36mUser Load (0.6ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = x'145a435c66324d54aca35e834b9e2d41' ORDER BY `users`.`id` ASC LIMIT 1[0m
Rendered administrators/adminview.html.erb within layouts/admin (13.7ms)
Rendered application/_navigation.html.erb (1.4ms)
Rendered application/_admin_navigation.html.erb (1.8ms)
Completed 200 OK in 212ms (Views: 206.9ms | ActiveRecord: 0.6ms)
Started GET "/administrators/145a435c-6632-4d54-aca3-5e834b9e2d41/manage_accounts" for 127.0.0.1 at 2015-04-01 07:58:45 -0400
Processing by AdministratorsController#manage_accounts as HTML
Parameters: {"id"=>"145a435c-6632-4d54-aca3-5e834b9e2d41"}
[1m[35mUser Load (0.6ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = x'145a435c66324d54aca35e834b9e2d41' ORDER BY `users`.`id` ASC LIMIT 1
[1m[36m (49.0ms)[0m [1mSELECT COUNT(*) FROM `accounts`[0m
[1m[35mAccount Load (11.8ms)[0m SELECT `accounts`.* FROM `accounts` ORDER BY id LIMIT 15 OFFSET 0
[1m[36mCustomer Load (12.7ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 490181591 LIMIT 1[0m
[1m[35mCustomer Load (0.7ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 189365990 LIMIT 1
[1m[36mCustomer Load (0.5ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 24420774 LIMIT 1[0m
[1m[35mCustomer Load (0.5ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 772684056 LIMIT 1
[1m[36mCustomer Load (0.5ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 862455622 LIMIT 1[0m
[1m[35mCustomer Load (0.4ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 417734356 LIMIT 1
[1m[36mCustomer Load (0.4ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 220490343 LIMIT 1[0m
[1m[35mCustomer Load (0.4ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 685817728 LIMIT 1
[1m[36mCustomer Load (0.4ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 53991993 LIMIT 1[0m
[1m[35mCustomer Load (0.4ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 676540929 LIMIT 1
[1m[36mCustomer Load (0.4ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 726000565 LIMIT 1[0m
[1m[35mCustomer Load (0.4ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 554199658 LIMIT 1
[1m[36mCustomer Load (0.6ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 767280416 LIMIT 1[0m
[1m[35mCustomer Load (0.6ms)[0m SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 237301229 LIMIT 1
[1m[36mCustomer Load (0.6ms)[0m [1mSELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 209732030 LIMIT 1[0m
Rendered administrators/_manage_accounts.html.erb (194.7ms)
Completed 200 OK in 217ms (Views: 132.9ms | ActiveRecord: 80.8ms)
而且,這裏是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'
get 'home/about'
get 'home/help'
get 'accounts/add_account', to: 'accounts#new'
post 'accounts/add_account', to: 'accounts#create'
resources :administrators do
member do
get :adminview
get :manage_accounts
get :manage_customers
get :manage_acct_transactions
end
end
resources :users do
resource :customers
resource :accounts
resource :addresses
end
resources :accounts do
resource :acct_transactions
end
resources :account_types, :accounts, :addresses, :administrators, :customers, :transaction_types, :acct_transactions, :users
end
再次,根據日誌,它應該是加載部分_manag e_accounts,但我只看到一個空白頁面。我試過使用路徑助手,但它會拋出一個「Missing Template」錯誤。嘗試了很多其他的東西,而不是操作方法,但我仍然得到相同的結果。
你沒有把主要'yield' – 2015-04-01 03:54:38
@MohammadAbuShady在HTML塊以上,我已經把'<%=產量:admin_content%>'。我也試過只是簡單''<%= yield %>但沒有工作,要麼 – 2015-04-01 03:57:10
什麼做日誌說? – vee 2015-04-01 04:01:15