0

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

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

当我编辑 news/1/edit 并上传图片时,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" %>

新闻/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

这是我的模型

漫画评论.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

新闻.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 的照片都会获得与 news 相同的照片

任何帮助将不胜感激谢谢

4

2 回答 2

0

好的,所以我要做的是在我的模型中我改变了路径

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

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

它似乎正在工作

于 2012-12-14T21:23:36.033 回答
0

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

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

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

于 2012-12-14T20:45:15.570 回答