回形针显示缺失的图像而不是定义的图像

时间:2016-07-24 20:04:16

标签: ruby-on-rails paperclip

显示所有用户的照片有效,但是显示一个用户的特定照片不显示(显示缺少的图像而不是通过ID定义的照片)。获取特定图像的路线为/users/:user_id/photos/:id。这是我的照片控制器:

class PhotosController < ApplicationController

    before_action :find_user, only: [:index, :new, :create, :update]
    before_action :find_photo, only: [:show, :destroy]

    def index
        @photos = @user.photos
    end

    def new
        @photo = @user.photos.build
    end

    def show
    end

    def create
        @photo = Photo.new(photo_params)

        if @photo.save

            if params[:images]
                params[:images].each { |image|
                    @user.photos.create(image: image)
                }
            end

            redirect_to :action => :index

        else
            render 'new'
        end
    end

    def update
        @photo = @user.photos.find(params[:id])

        if @photo.update
            redirect_to user_photos
        else
            render 'edit'
        end
    end

    def destroy
        @photo.destroy

        redirect_to user_photos
    end

    private

    def find_user
        @user = User.find(params[:user_id])
    end

    def find_photo
        @photo = Photo.find(params[:id])
    end

    def photo_params
        params.require(:photo).permit(:title, :image, :user_id)
    end
end

这是我的用户控制器:

class UsersController < ApplicationController

    def index
        @search = User.search(params[:q])

        if @search
            @users = @search.result.paginate(:per_page => 10, :page => params[:strana])
        else
            @users = User.all.paginate(:per_page => 10, :page => params[:strana]).order("created_at DESC")
        end
    end

    def new
        @user = User.new
    end

    def create
        @user = User.new(user_params)

        if @user.save
            redirect_to @user
        else
            render 'new'
        end     
    end

    def show
        find_params
        @photos = @user.photos
    end

    def edit
        find_params
    end

    def update
        find_params

        if @user.update(user_params)
            redirect_to @user
        else
            render 'edit'
        end
    end

    def destroy
        find_params
        @user.destroy

        redirect_to users_path
    end

这是照片/节目视图:

<%= image_tag @photo.image.url(:thumb) %>

这是照片/索引视图:

<% @user.photos.each do |photo| %>

    <%= image_tag(photo.image) %>
    <%= photo.title %>

<% end %>

路由是嵌套的:

resources :users do
    resources :photos
end

1 个答案:

答案 0 :(得分:1)

问题解决了 - 第一张图片实际上没有上传(我忘了添加验证)所以它们在图片链接字段中没有值。