Rails 4 content_for和yield显示空白页面

时间:2015-04-01 03:44:20

标签: ruby-on-rails render yield actionview 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

同样,根据日志,它应该加载部分_manage_accounts,但我只看到一个空白页面。我尝试过使用路径助手,但它会抛出一个&#34; Missing Template&#34;错误。尝试了很多其他的东西,而不是动作方法,但我仍然得到相同的结果。

1 个答案:

答案 0 :(得分:3)

为什么要将局部视图渲染为您的操作的视图?

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设计。