簡體   English   中英

沒有將 nil 隱式轉換為 String Ruby on Rails

[英]no implicit conversion of nil into String Ruby on Rails

有人可以幫忙嗎?

我的博文分享未在 Twitter 上顯示圖像。 我對其他網站進行了基准測試,並注意到所有博客文章工作網站在圖像 URL 之前都有一個域 URL。 所以,我添加了它,博客文章開始工作了!!!。

然后,我遇到了另一個問題。 當我點擊查看博客頁面時,它會顯示一條錯誤消息(如下所示)

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

21:   %meta{:content => "https://www.joynus.com"+@post.preview_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

有沒有辦法跳過此錯誤消息? 我做了一些研究,發現開始/救援。 但我不知道如何以及在哪里放置它。

它真的很感激任何幫助或建議。

這是因為您使用+將 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"+@post.preview_image.try(:data).try(:url).to_s, :name => "twitter:image"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM