简体   繁体   English

Rails部分集合未呈现

[英]Rails partials for collections not rendering

I am following a tutorial and we are creating a partial to to display a collection of data, but it is not rendering, my rails server command prompt shows that the database transaction is succesful: 我正在关注一个教程,我们正在创建一个部分显示数据集合,但它没有渲染,我的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)

Here is my partial under portfolios/_portfolio_item.html.erb: 这是我的部分投资组合/ _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?' } %>

My index view under portfolios/index.html.erb : 我在投资组合/ index.html.erb下的索引视图:

<h1>Portfolio Items</h1>

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

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

And my portfolios_controller : 而我的portfolio_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

If there is anything further needed please let me know and thanks in advance! 如果还有其他需要请提前告知我们并提前致谢!

I'm not familiar with the collection syntax bbut for a quick fix try: 我不熟悉收集语法bbut快速修复尝试:

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

Or if you really want to be fancy, and your model is called Portfolio, rename your partial to portfolios/_portfolio.html.erb . 或者,如果您真的想要花哨,并且您的模型名为Portfolio,请将您的部分重命名为portfolios/_portfolio.html.erb That will let you do: 那会让你做到:

## index.html.erb

<h1>Portfolio Items</h1>

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

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

Make sure there is no CSS or javascript that is removing/hiding your views. 确保没有删除/隐藏您的视图的CSS或JavaScript。 Also make sure you actually have some instances(rows) of the portfolio in your database. 还要确保您的数据库中实际上有一些组合的实例(行)。

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

Although your usage of collection in partial is correct you can try to pass local variables and see if it works, because collection usage is dependent on file/model names which could cause issues. 尽管您在partial中使用集合是正确的,但您可以尝试传递局部变量并查看它是否有效,因为集合使用取决于可能导致问题的文件/模型名称。

When you use collection: @portfolio_items then you get access to use @portfolio_items instance variable within the partial view ( portfolios/_portfolio_item.html.erb in your case). 当您使用collection: @portfolio_items您可以访问部分视图中的@portfolio_items实例变量(在您的情况下为portfolios/_portfolio_item.html.erb )。 You are passing @portfolio_items but not using it anywhere, instead you are using portfolio_item variable which is not available in the partial view. 您正在传递 @portfolio_items但未在任何地方使用它,而是使用在部分视图中不可用的 portfolio_item变量。

You need to iterate over @portfolio_items in order to render your items. 您需要迭代@portfolio_items才能呈现您的项目。 If you need to pass variables to your partial use locals . 如果需要将变量传递给部分使用的locals变量。 Update your portfolios/index.html.erb as below 更新您的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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM