Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
没有将nil隐式转换为字符串Ruby-on-Rails_Ruby On Rails_Ruby_Haml_Meta Tags - Fatal编程技术网

没有将nil隐式转换为字符串Ruby-on-Rails

没有将nil隐式转换为字符串Ruby-on-Rails,ruby-on-rails,ruby,haml,meta-tags,Ruby On Rails,Ruby,Haml,Meta Tags,有人能帮忙吗 我的博客帖子共享在Twitter上没有显示图像。我对其他网站进行了基准测试,发现所有博客帖子工作网站在图片URL之前都有一个域URL。所以,我在上面加了一个,博客帖子开始工作了!!!。耶 然后,我遇到了另一个问题。当我点击查看博客页面时,它会显示一条错误消息(根据下面的) 我的邮政管理员 class PostsController < ApplicationController before_filter :authorize, only: [:edit, :updat

有人能帮忙吗

我的博客帖子共享在Twitter上没有显示图像。我对其他网站进行了基准测试,发现所有博客帖子工作网站在图片URL之前都有一个域URL。所以,我在上面加了一个,博客帖子开始工作了!!!。耶

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

我的邮政管理员

 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
class PostsControllerfalse}
结束
结束
def秀
结束
def新
@post=post.new
结束
def创建
@post=post.new(post_参数)
如果@post.save
将\u重定向到帖子\u url
其他的
呈现“新”
结束
结束
定义编辑
结束
def更新
如果@post.update_属性(post_参数)
将_重定向到post_url(@post),注意:“#{@post.title}已更新”
其他的
渲染“编辑”
结束
结束
def销毁
@事后销毁
重定向到帖子的url,注意:“{@post.title}已删除”
结束
私有的
def post_参数
参数require(:post).permit(:title,:social_title,:contents,:author_id,:approved,:summary,:preview_image_id,:category)
结束
def find_post
@post=post.friendly.find(参数[:id])
#如果使用旧id或数字id查找记录,则
#请求路径与post_路径不匹配,我们应该这样做
#使用当前友好id的301重定向。
如果参数[:操作]='show'&&request.path!=post_路径(@post)
返回重定向到@post,:status=>:moved\u永久
结束
结束
结束
还有,我的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
class Post
有没有办法跳过此错误消息?我做了一些研究,发现了begin/rescue。但我不知道怎么放,也不知道放在哪里


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

这是因为您正在使用
+
将URL隐式连接到主机,但至少有一篇文章,
@post.preview\u image.try(:data)。try(:URL)
返回为
nil

您可以通过如下方式使用字符串插值来修复它:

%meta{:content => "https://www.joynus.com#{@post.preview_image.try(:data).try(:url)}", :name => "twitter:image"}
%meta{:content => "https://www.joynus.com"+@post.preview_image.try(:data).try(:url).to_s, :name => "twitter:image"}
或者使用
将字符串显式转换为_s
,如下所示:

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

第二段告诉我们。。。什么?请简明扼要。“”及其链接页面和“”都是有用的阅读资料。或者在最近的红宝石中:
@post.preview\u image&.data&.url
@Unixmonkey您是最好的!