出现在错误地方的纸夹图象

时间:2012-12-14 20:25:36

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

嘿,我有一个rails应用程序,我用paperclip上传图像....

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

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

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

继承人的观点

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

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

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

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

我很确定问题是你在两个型号中都给出了“照片”的相同名称。尝试命名一个comic_photo和另一个news_photo,或者替代地,探索多态关联。

Dated Railscast链接,但为您提供了想法 - http://railscasts.com/episodes/154-polymorphic-association

PS - 用于确定如何在一个查询中加载四个ComicReviews而不是在这些控制器中加载四个ComicReviews的奖励点。

答案 1 :(得分:0)

好的,所以我必须做的是在我的模型中,我改变了

的路径

:path =&gt; “:连接/活动/:ID /:样式:扩展名”,

:path =&gt; “/图片/:ID /:文件名”,

似乎正在运作