2016-07-24 3 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

문제가 해결 - 첫 실제로 이미지가 업로드되지 않았다 (I 유효성 검사를 추가하는 것을 잊었다) 그래서 그들은 이미지 링크 필드에 전무 값을했다.

+0

대기중인 질문 대기열에서 응답을 녹색 눈금으로 보내십시오. – user2792268

+0

지금 당장은 할 수 없습니다 - 내 대답을 받아들이려면 하루 더 기다려야합니다. – Nikola