Rails局部集合未渲染

时间:2019-05-09 17:13:26

标签: ruby-on-rails ruby

我正在遵循一个教程,我们正在创建一个局部视图以显示数据集合,但未呈现,我的rails服务器命令提示符显示数据库事务成功:

Started GET "/portfolios" for ::1 at 2019-05-09 11:52:03 -0500
Processing by PortfoliosController#index as HTML
  Rendering portfolios/index.html.erb within layouts/portfolio
  Portfolio Load (0.3ms)  SELECT "portfolios".* FROM "portfolios"
  ↳ app/views/portfolios/index.html.erb:5
  Rendered collection of portfolios/_portfolio_item.html.erb [11 times] (2.1ms)
  Rendered portfolios/index.html.erb within layouts/portfolio (5.3ms)
  Rendered shared/_nav.html.erb (0.8ms)
Completed 200 OK in 31ms (Views: 28.6ms | ActiveRecord: 0.3ms)

这是我在Portfolios / _portfolio_item.html.erb下的部分工作:

<p><%= link_to portfolio_item.title, portfolio_show_path(portfolio_item) %></p>

<p><%= portfolio_item.subtitle %></p>

<p><%= portfolio_item.body %></p>

<%= image_tag portfolio_item.thumb_image unless portfolio_item.thumb_image.nil? %>
<%= link_to "Edit", edit_portfolio_path(portfolio_item) %>
<%= link_to 'Delete Portfolio Item', portfolio_path(portfolio_item), method: :delete, data: { confirm: 'Are you sure?' } %>

Portfolios / index.html.erb下的我的索引视图:

<h1>Portfolio Items</h1>

<%= link_to "Create New Item", new_portfolio_url %>

<%= render partial: 'portfolio_item', collection: @portfolio_items %>

还有我的Portfolios_controller:

class PortfoliosController < ApplicationController
  layout 'portfolio'

  def index
    @portfolio_items = Portfolio.all
  end

  def show
    @portfolio_item = Portfolio.find(params[:id])
  end

  def new
    @portfolio_item = Portfolio.new
    3.times { @portfolio_item.technologies.build }
  end

  def create
    @portfolio_item = Portfolio.new(portfolio_params)

    respond_to do |format|
      if @portfolio_item.save
        format.html { redirect_to portfolios_path, notice: 'Your portfolio item is now live!' }
      else
        format.html { render :new }
      end
    end
  end

  def edit
    @portfolio_item = Portfolio.find(params[:id])
  end

  def update
    @portfolio_item = Portfolio.find(params[:id])
    respond_to do |format|
      if @portfolio_item.update(portfolio_params)
        format.html { redirect_to portfolios_path, notice: 'The record was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  def destroy
    @portfolio_item = Portfolio.find(params[:id])
    @portfolio_item.destroy
        respond_to do |format|
      format.html { redirect_to portfolios_url, notice: 'Portfolio was successfully destroyed.' }
    end
  end

  private

  def portfolio_params
    params.require(:portfolio).permit(:title,
                                      :subtitle,
                                      :body,
                                      :main_image,
                                      :thumb_image,
                                      technologies_attributes: [:name])
  end
end

如果还有其他需要,请提前通知我!

2 个答案:

答案 0 :(得分:0)

我不熟悉集合语法,但是尝试快速修复:

## index.html.erb

<h1>Portfolio Items</h1>

<%= link_to "Create New Item", new_portfolio_url %>

<% @portfolio_items.each do |item| %>
  <%= render partial: 'portfolio_item', portfolio_item: item %>
<% end %>

或者,如果您真的想花哨,并且您的模型称为Portfolio,则将您的部分重命名为portfolios/_portfolio.html.erb。这样您就可以做到:

## index.html.erb

<h1>Portfolio Items</h1>

<%= link_to "Create New Item", new_portfolio_url %>

<% @portfolio_items.each do |item| %>
  <%= render item %>
<% end %>

答案 1 :(得分:0)

确保没有任何CSS或javascript会删除/隐藏您的视图。另外,还要确保您的数据库中确实有投资组合的某些实例(行)。

<%= render partial: 'portfolio_item', collection: @portfolio_items %>

尽管部分使用集合是正确的,但是您可以尝试传递局部变量并查看其是否有效,因为集合的使用取决于可能引起问题的文件/模型名称。

使用collection: @portfolio_items时,您可以访问部分视图中的@portfolio_items实例变量(在您的情况下为portfolios/_portfolio_item.html.erb)。您正在传递@portfolio_items但不在任何地方使用它,而是使用portfolio_item变量,该变量在局部视图中不可用。

您需要遍历@portfolio_items才能渲染项目。如果您需要将变量传递给部分变量,请使用locals。如下更新您的portfolios/index.html.erb

<h1>Portfolio Items</h1>

<%= link_to "Create New Item", new_portfolio_url %>

<% @portfolio_items.each do |portfolio_item| %>
    <%= render partial: 'portfolio_item', locals: { portfolio_item: portfolio_item } %>
<% end %>