【问题标题】:Paper clip image showing up in wrong place回形针图像出现在错误的位置
【发布时间】:2012-12-14 20:25:36
【问题描述】:

嘿,我有一个 Rails 应用程序,我正在用回形针上传图像....

所以我有一个comic_review 脚手架,我可以上传漫画封面的图片,我还有一个新闻脚手架,我可以在其中发布新闻和图片.....

当我编辑 news/1/edit 并上传图片时,comics_review/1 图片与新闻图片相同,反之亦然。

我检查了视图中的链接以确保 i 是正确的并且它们似乎没问题...

这是我的看法

home.html.erb

          <h2 style="color: black; font-size: 30px;"><%= @comic_review1.title %></h2>
          <p><%= @comic_review1.content %></p>
          <%= link_to 'Read More...', comic_review_path(@comic_review1) %>               
          <%= image_tag @comic_review1.photo, class: "homepage_comics" %>

新闻/show.html

          <%= @news.author %>
          <%= @news.date %>
          <%= image_tag @news.photo, class: 'pull-left', style: 'margin-right: 10px;' %>

这是我的控制器

static_pages_controller.rb

           class StaticPagesController < ApplicationController
           def home
         @user = current_user
             @article = Article.first
             @comic_review1 = ComicReview.find(1)
             @comic_review2 = ComicReview.find(2)
             @comic_review3 = ComicReview.find(3)
             @comic_review4 = ComicReview.find(4)
           end

           def comics
             @comic_review1 = ComicReview.find(1)
             @comic_review2 = ComicReview.find(2)
             @comic_review3 = ComicReview.find(3)
             @comic_review4 = ComicReview.find(4)
           end

           def batnews
             @article = Article.first
             @news_all = News.all
           end
           end

这是我的模型

comic_review.rb

        class ComicReview < ActiveRecord::Base
        attr_accessible :content, :credits, :review, :title, :photo, :comicreview
        has_attached_file :photo, :styles => { :small => '400x400' },
        :storage => :s3,
        :s3_credentials => "#{Rails.root}/config/s3.yml",
        :path => ":attachment/activities/:id/:style.:extension",
        :bucket => 'goddam_batman_pics'

        has_many :comments, as: :commentable, dependent: :destroy
        end

news.rb

        class News < ActiveRecord::Base
        attr_accessible :title, :author, :date, :content, :photo
        has_attached_file :photo, :styles => { :small => '400x400' },
       :storage => :s3,
       :s3_credentials => "#{Rails.root}/config/s3.yml",
       :path => ":attachment/activities/:id/:style.:extension",
       :bucket => 'goddam_batman_pics'
       end

控制器是由脚手架生成的基本控制器

所以每当我为 news/1 更新照片时,comic_reviews/1 的照片都会得到与新闻照片相同的照片

任何帮助将不胜感激,谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rubygems paperclip


    【解决方案1】:

    我很确定问题在于您在两个模型中为“照片”赋予了相同的名称。尝试命名一张comic_photo 和另一张news_photo,或者探索多态关联。

    过时的 Railscast 链接,但给了你想法 - http://railscasts.com/episodes/154-polymorphic-association

    PS - 弄清楚如何在一个查询中加载四个 ComicReview 而不是在这些控制器中加载四个的奖励积分。

    【讨论】:

    • 这是一个很好的观点,我在创建 cmets 时检查了多态关联的东西.....但是我也有一个文章模型,我也在使用照片,它似乎没有任何问题
    • 为两种不同的型号尝试不同的:path
    • 我认为这与“照片”名称无关,因为它们都是不同的表,对吧?因此,如果我有一个名为 something1 的表,其字段为名称,另一个表为 something2,其字段为名称,则每个表都应该是唯一的
    • 所以我猜它与亚马逊 s3 有关,我删除了代码,一切正常
    • 我不确定如何更改路径
    【解决方案2】:

    好的,所以我必须在我的模型中更改路径

    :path => ":attachment/activities/:id/:style.:extension",

    :path => "/image/:id/:filename",

    它似乎正在工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多