Rails 4 content_for和yield显示空白页

时间:2021-01-15 21:11:40

I'm trying to render the following partial:

我试图呈现以下部分:

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

(there are other similar partials as well)

(还有其他类似的偏置)

... in the following layout:

…在接下来的布局:

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

The reason I'm trying to use content_for is that I have a couple of other pages similar to the above partial that I want to render when a link is clicked in the navigation:

我使用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>

The above links call respective methods in the controller, which are as follows:

上面的链接调用控制器中的相应方法,如下所示:

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

But when you click the links in navigation, it just shows a blank page with no html at all. The console says that the objects are loaded as per the controller, and that the partial is being rendered. But where is it?

但是当你点击导航中的链接时,它只显示一个没有html的空白页面。控制台说,对象是按控制器加载的,而部分正在被呈现。但是在哪里?

I've tried all kinds of ways to get this to work. The closest I got was actually without the content_for/yield and just calling the method, which of course rendered the partial as if it were its own page (bad). There used to be something called replace_html which would probably work for what I'm trying to do but I'm using Rails 4.1.8.

我已经尝试了各种方法来让它工作。我得到的最接近的结果是,没有content_for/yield,只调用方法,当然,它将部分呈现为自己的页面(不好)。曾经有一种叫做replace_html的东西可能对我要做的事情有用但是我用的是Rails 4。1。8。

Initially, I set this navigation up with AJAX, but it just doesn't work here. These partials have links for CRUD actions as well as redirects. Using AJAX led to all kinds of CSRF errors and seems like it adds an unnecessary layer of complexity for something that should be pretty simple.

最初,我使用AJAX设置了这个导航,但在这里不工作。这些部分具有CRUD操作和重定向的链接。使用AJAX会导致各种类型的CSRF错误,似乎它会给本来应该相当简单的东西增加不必要的复杂性。

Why does the above code not render anything?

为什么上面的代码不呈现任何内容?

Am I taking the wrong approach?

我采用了错误的方法吗?

Thanks

谢谢

EDIT: adding logs for the page request..

编辑:为页面请求添加日志。

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)

Also, here is the routes.rb if that helps solve this problem..

还有,这是路线。如果这有助于解决这个问题。

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

Again, according to logs, it supposedly is loading the partial _manage_accounts, but I'm only seeing a blank page. I've tried using path helper but it throws a "Missing Template" error. Have tried lots of other things, instead of the action method but I still get the same result.

同样,根据logs,它可能正在加载部分_manage_accounts,但我看到的只是一个空白页面。我尝试过使用路径助手,但它会抛出一个“丢失的模板”错误。我尝试了很多其他的方法,而不是动作方法,但我还是得到了相同的结果。

1 个解决方案

#1


3  

Why are you rendering partials as the views for your actions?

为什么要把偏置作为行为的视图?

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

Calling render partial like this will only render the content of the partial and will not load the layout. If you want the layout (and it certainly sounds like you do) then rename this file to a normal view app/views/administrators/manage_accounts.html.erb and then remove the render call from your action altogether.

像这样调用呈现部分将只呈现部分的内容,而不会加载布局。如果您想要布局(当然,这听起来很像),那么将这个文件重命名为一个普通的视图应用程序/视图/管理员/manage_accounts.html。然后从动作中删除渲染调用。

I would also advise splitting each of these manage routes out into their own controllers, so instead you would have app/views/admin/accounts/index.html.erb, which would become the new view to manage accounts. I suggest this because it falls in line with the more traditional CRUD design of a Rails application.

我还建议将这些管理路由分成各自的控制器,所以您应该使用app/view /admin/account /index.html。erb,这将成为管理账户的新视角。我建议这样做是因为它符合Rails应用程序的更传统的CRUD设计。

#1


3  

Why are you rendering partials as the views for your actions?

为什么要把偏置作为行为的视图?

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

Calling render partial like this will only render the content of the partial and will not load the layout. If you want the layout (and it certainly sounds like you do) then rename this file to a normal view app/views/administrators/manage_accounts.html.erb and then remove the render call from your action altogether.

像这样调用呈现部分将只呈现部分的内容,而不会加载布局。如果您想要布局(当然,这听起来很像),那么将这个文件重命名为一个普通的视图应用程序/视图/管理员/manage_accounts.html。然后从动作中删除渲染调用。

I would also advise splitting each of these manage routes out into their own controllers, so instead you would have app/views/admin/accounts/index.html.erb, which would become the new view to manage accounts. I suggest this because it falls in line with the more traditional CRUD design of a Rails application.

我还建议将这些管理路由分成各自的控制器,所以您应该使用app/view /admin/account /index.html。erb,这将成为管理账户的新视角。我建议这样做是因为它符合Rails应用程序的更传统的CRUD设计。