【问题标题】:rails active storage wrong url - 301 statusrails active storage 错误的 url - 301 状态
【发布时间】:2019-10-24 09:39:32
【问题描述】:

使用活动存储将image 附加到article

storage.yml

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

article.rb

class Article < ApplicationRecord
  belongs_to :auteur
  has_one_attached :image

在添加文章的表格中:

<%= f.file_field :image %>

在 article_controller.rb 中:

def hop
    @auteur = Auteur.find(params[:auteur_id])
    @article = @auteur.articles.new(article_params)
    @article.publie = params[:publie].to_s
    @article.slug = Article.to_slug(@article.titre)
    @article.save
    redirect_to adm_auteur_tous_articles_url
  end

这样,图片就会上传到storage/es/ul/esulvqpj2apfcqx41hwdtnchd5y4

(这是正确的,因为我可以在 MacOs finder 窗口中看到图像)

但在视图中,我这样做:

&lt;%= image_tag(@article.image) if @article.image.attached? %&gt;

图片没有显示,因为图片的网址是:

http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--661c66c418b20fc46350823faaac7124c25138dd/chat.jpg

这会导致301 moved permanently 状态。

所以图像没有显示出来。

【问题讨论】:

  • 你说得对:我删除了控制器中的显式附件(也不再出现在问题中)。我也做其他事情,比如创建一个 slug(与活动存储无关)

标签: ruby-on-rails rails-activestorage


【解决方案1】:

从路由中删除这一行就可以了(我把它作为最后一行 ot routes.rb:

 get '*path' => redirect('/') 

【讨论】:

    猜你喜欢
    • 2021-09-20
    • 2019-01-26
    • 2022-11-28
    • 1970-01-01
    • 2012-05-08
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    相关资源
    最近更新 更多