2015-04-01 56 views
3

我試圖呈現以下部分:軌道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」錯誤。嘗試了很多其他的東西,而不是操作方法,但我仍然得到相同的結果。

+0

你沒有把主要'yield' – 2015-04-01 03:54:38

+0

@MohammadAbuShady在HTML塊以上,我已經把'<%=產量:admin_content%>'。我也試過只是簡單''<%= yield %>但沒有工作,要麼 – 2015-04-01 03:57:10

+0

什麼做日誌說? – vee 2015-04-01 04:01:15

回答

3

你爲什麼要渲染partials作爲你的動作視圖?

def manage_accounts 
    @accounts = Account.order('id').page(params[:page]).per(15) 
    render partial: 'manage_accounts' 
end 

調用render partial這樣只會渲染部分的內容,並不會加載的佈局。如果你想要的佈局(它當然聽起來像你這樣做),那麼這個文件重命名爲普通視圖app/views/administrators/manage_accounts.html.erb,然後完全刪除您的行動render電話。

我還建議將這些manage路由中的每一個拆分到它們自己的控制器中,相反,您將擁有app/views/admin/accounts/index.html.erb,這將成爲管理帳戶的新視圖。我建議這是因爲它符合Rails應用程序的更傳統的CRUD設計。

+0

完美!我不知道該怎麼感謝你才足夠。按照你所說的重新命名了文件(沒有下劃線前綴)。然後,從管理方法中刪除單詞「partial:」並且presto!一切都很好。至於其他的建議,我_would_做這種方式,但我的用戶也將有機會獲得這些控制器方法(帳戶等。)而這些「管理」的方法是嚴格的管理員。所以我試圖做的是保持抽象到這個管理員控制器的方法。整個控制器(以及其中的一切)都需要授權。再次感謝 - 你是超人! – 2015-04-01 22:43:35

+1

@ B.Bulpett而不是有一個'管理員'控制器,我會將這些行爲移動到一個管理員命名空間。我在這裏有一個指導:https://github.com/radar/guides/blob/master/admin-namespace.md。我認爲,如果你將這些行爲控制在一個控制器中,那麼控制器將會承擔太多的責任並且難以管理。通過將其分解爲多個控制器,您可以使整個事情更容易管理。 – 2015-04-06 21:42:15

+0

謝謝!如果我再做一次,我肯定會採取這種方法。我在這個兔子洞下面有點太遠了。感謝您提供這個偉大的建議! – 2015-04-07 23:26:59