回形针图像显示在错误的位置


嘿,我

有一个 rails 应用程序,我正在上传带有回形针的图像......

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

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

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

这是我的观点

首页.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" %>

news/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

新闻网

        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
控制器

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

因此,每当我更新新闻/1 的照片时,comic_reviews/1 的照片都会获得与新闻相同的照片

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

我很确定问题是您在两种模型中都为"照片"提供了相同的名称。 尝试命名一个comic_photo,另一个news_photo,或者探索多态关联。

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

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

好的,

所以我要做的是在我的模型中,我将路径从

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

:p ath => "/image/:id/:filename",

它似乎正在工作

最新更新