2016-07-24 37 views
0

显示所有用户的照片的作品,但显示一个用户的特定照片不显示(显示丢失的图像,而不是由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 
+0

如果您试图显示特定用户的所有照片,它应该位于用户显示页面而不是照片/显示中。 –

+0

那么,我的应用程序的结构有点不同:在用户/节目是用户信息,并且有一个按钮_Photos_发送到该用户的照片的索引。无论如何,这并不会改变任何事情,因为我仍然需要调用'/ users /:user_id/photos /:id'来查看特定照片。 – Nikola

回答

1

问题就解决了 - 第一个图像实际上没有上传(我忘了添加验证),使他们不得不在图像链接领域零值。

+0

给你的答案绿色滴答从等待问题队列中删除它。 – user2792268

+0

我现在无法做到 - 我需要再等一天才能接受我自己的答案。 – Nikola

相关问题