没有将nil隐式转换为Rails中的String Ruby

问题描述 投票:0回答:1

有人可以帮忙吗?

我的博客文章共享未在Twitter上显示图像。我对其他网站进行了基准测试,发现所有博客文章工作网站的域名URL都在图片URL之前。因此,我添加了它,博客文章开始工作!!! Yayy

然后,我遇到了另一个问题。当我单击以查看博客页面时,它显示一条错误消息(每个下面)

ActionView::Template::Error (no implicit conversion of nil into String):

21:   %meta{:content => "https://www.joynus.com"[email protected]_image.try(:data).try(:url), :name => "twitter:image"}

我的帖子管理员

 class PostsController < ApplicationController
  before_filter :authorize, only: [:edit, :update, :new, :create, :destroy]
  before_filter :find_post, only: [:show, :edit, :update, :destroy]

  def index
    @posts = Post.order("created_at DESC").page(params[:page]).per(9)

    respond_to do |format|
      format.html
      format.rss { render :layout =>false }
    end
  end

  def show

  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)

    if @post.save
      redirect_to posts_url
    else
      render 'new'
    end
  end

  def edit

  end

  def update
    if @post.update_attributes(post_params)
      redirect_to post_url(@post), notice: "#{@post.title} Updated"
    else
      render 'edit'
    end
  end

  def destroy
    @post.destroy

    redirect_to posts_url, notice: "#{@post.title} Deleted"
  end

  private

  def post_params
    params.require(:post).permit(:title, :social_title, :contents, :author_id, :approved, :summary, :preview_image_id, :category)
  end

  def find_post
    @post = Post.friendly.find(params[:id])

    # If an old id or a numeric id was used to find the record, then
    # the request path will not match the post_path, and we should do
    # a 301 redirect that uses the current friendly id.
    if params[:action] == 'show' && request.path != post_path(@post)
      return redirect_to @post, :status => :moved_permanently
    end
  end
end

而且,我的post.rb

 class Post < ActiveRecord::Base
  extend FriendlyId
  friendly_id :slug_candidates, use: [:slugged, :history]

  belongs_to :preview_image, class_name: 'Ckeditor::Picture'
  belongs_to :author, class_name: 'User'

## Validations
  validates :contents, presence: true
  validates :title, presence: true
  validates :social_title, presence: true
  validates :summary, presence: true, length: 1..300
  validates :author, presence: false
  validates :category, presence: true
  delegate :full_name, to: :author, prefix: true, allow_nil: false

## Instance Methods
  def slug_candidates
    [
      :slug_title,
      [:id, :slug_title]
    ]
  end

  def slug_title
    title&.downcase
  end

  def should_generate_new_friendly_id?
      title_changed?
  end

  def raw_post
    self.contents.html_safe
  end

  def preview_image_thumb(dimensions = '100x')
    preview_image.try(:data).try(:thumb, dimensions).try(:url)
  end

  def self.preview_image_dimensions
    '350x'
  end
end

是否有跳过此错误消息的方法?我做了一些研究,发现开始/救援。但是我不知道如何以及在哪里放置它。

非常感谢您的任何帮助或建议。

ruby-on-rails ruby haml meta-tags
1个回答
0
投票

这是因为您正在使用+将URL隐式连接到您的主机,但是至少对于一则帖子,@post.preview_image.try(:data).try(:url)返回为nil

您可以通过像这样使用字符串插值来解决它:

%meta{:content => "https://www.joynus.com#{@post.preview_image.try(:data).try(:url)}", :name => "twitter:image"}

或通过像这样用to_s显式转换为字符串:

%meta{:content => "https://www.joynus.com"[email protected]_image.try(:data).try(:url).to_s, :name => "twitter:image"}
© www.soinside.com 2019 - 2024. All rights reserved.