简体   繁体   中英

no implicit conversion of nil into String Ruby on Rails

can somebody help with this, please?

My blog post share isn't showing an image on Twitter. I benchmarked other websites and noticed all the blog post working websites had a domain URL in the prior to the image URL. So, I added on it and blog post started working!!!. Yayy

Then, I've encountered another problem. When I click to see a blog page, it shows an error message(per below)

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"}

My post controller

 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

And, my 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

Is there a way to skip this error message? I did some research and found begin/rescue. But I don't know how and where to put it.

It would really appreciate any help or advice.

This is because you are using + to implicitly concatenate the URL to your host, but at least for one post, @post.preview_image.try(:data).try(:url) is returning as nil .

You could fix it by using string interpolation like this:

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

Or by explicitly converting to string with to_s like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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