在Rails中基于数据数组生成动态元标记

时间:2018-01-17 18:30:05

标签: ruby-on-rails

我有关于书籍的以下数据。

[{
  title: "Gulliver's Travels",
  description: "Pretentious guy travels around a lot in Victorian England. Decides he doesn't like people.",
  image: 'someguy.jpg',
  name: 'gtravels'
 },
 {
  title: "Wizard of Oz",
  description: "Little girl travels to another dimension, commits murder, overthrows the patriarchy, and brings her dog",
  image: 'anarchydoro.jpg',
  name: 'dtravels'
  }]

我继续为我的控制器创建了以下内容:

class BooksController < ApplicationController
  def index
   @books = Book.all
   @header = true
  end

  def order
   @book = Book.all
  end

  def show
   @book = Book.find(params[:id])
   @header = true
  end

我为每本书(节目)生成一个网页,包含所有书籍(索引)的页面,以及基于所有书籍(订单)的订购页面。凉。我还希望能够通过社交媒体分享数据,因此我创建了一个部分视图来处理所有视图的所有元标记(yay DRY)但它失败了。

所以在我的布局/书籍中我有:

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Books</title>
<%= stylesheet_link_tag 'application', :media => "all" %>
    <%= javascript_include_tag 'application' %>
        <%= csrf_meta_tags %>
            <%= yield :head %>
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
                <%= render partial: '/layouts/analytics' %>
                    <%= favicon_link_tag 'favicon.ico' %>
                        <%= render partial: '/books/metabooks'%>

在我所拥有的部分中:

<meta prefix="og: http://ogp.me/ns#" property="og:title" content="I like big books" />
<meta prefix="og: http://ogp.me/ns#" property="og:description" content="<%= books.description %>" />
<meta prefix="og: http://ogp.me/ns#" property="og:url" content="<%=request.original_url%>" />
<meta prefix="og: http://ogp.me/ns#" property="og:image" content="<%= @books.image %>" />


<!-- twitter meta tags -->
<meta prefix="og: http://ogp.me/ns#" name="twitter:card" content="summary_large_image" />
<meta prefix="og: http://ogp.me/ns#" name="twitter:site" content="@bookguy" />
<meta prefix="og: http://ogp.me/ns#" name="twitter:domain" content="@bookguy" />
<meta prefix="og: http://ogp.me/ns#" name="twitter:url" content="<%=request.original_url%>">
<meta prefix="og: http://ogp.me/ns#" name="twitter:title" content="<%= @books.title %>">
<meta prefix="og: http://ogp.me/ns#" name="twitter:description" content="<%= @books.description %>">
<meta prefix="og: http://ogp.me/ns#" property="twitter:image" content="<%= @books.image %>" />

我一直得到以下内容: 未定义的方法`描述&#39; for #Book :: ActiveRecord_Relation:0x007faab4063568&gt;

然后它打击了我,我试图通过一个阵列。那么如何根据网址生成元标记?

1 个答案:

答案 0 :(得分:0)

所以我如何为我解决它真的不是创建动态元标记的问题的确切答案。我专门为节目制作了一部分,然后是索引和顺序的另一部分。

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Books</title>
<%= stylesheet_link_tag 'application', :media => "all" %>
<%= javascript_include_tag 'application' %>
    <%= csrf_meta_tags %>
        <%= yield :head %>
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
            <%= render partial: '/layouts/analytics' %>
                <%= favicon_link_tag 'favicon.ico' %>
                   <% @metabook == true %>
                    <%= render partial: '/books/metabooks'%>
                      <% end %>