2011-05-06 7 views
0

uploads.Controller를 사용하여 uploads.Controls와 UsersController가 있는데, 여기에는 uploads.rb의 다형성 첨부 파일을 통해 Users ' has_many : uploads'가 있습니다.Rails-3 라우팅 문제

내가 http://localhost:3000/users/1/uploads

내가 업로드 번호 인덱스에 다시 라우팅 및 렌더링하려면 탐색 할 때 이제라고 여러 번 다음과 같이 :

Started GET "https://stackoverflow.com/users/1/uploads" for 127.0.0.1 at 2011-05-06 22:00:38 +0100 
Processing by UploadsController#index as HTML 
Parameters: {"user_id"=>"1"} 
[1m [35mUser Load (0.0ms) [0m SELECT "users".* FROM "users" WHERE 
"users"."id" = 1 LIMIT 1 
[1m [36mUpload Load (0.0ms) [0m [1mSELECT "uploads".* FROM 
"uploads" [0m 
Rendered uploads/_upload.html.erb (0.0ms) 
Rendered uploads/_upload.html.erb (0.0ms) 
Rendered uploads/_upload.html.erb (0.0ms) 
Rendered uploads/index.html.erb within layouts/application 

이 내 설정/경로

Uploader::Application.routes.draw do 
    devise_for :users 
    resources :users do 
    resources :uploads 
end 
root :to => 'users#index' 

class UsersController < ApplicationController 
    def show 
    @user = User.find 
    end 

views/users/show.html.erb 
<div> 
    <% @user.email %> 
    <h3 id="photos_count"><%= pluralize(@user.uploads.size, "Photo")%></h3> 
<div id="uploads"> 
    <%= image_tag @user.uploads.url(:small)%> 
    <em>on <%= @user.upload.created_at.strftime('%b %d, %Y at %H:%M') %></em> 
</div> 

<h3>Upload a Photo</h3> 
    <%= render "upload/form", :parent => @user, :upload => user.uploads.new %> 


class UploadsController < ApplicationController 
    def index 
    @uploads = Upload.all 
    @upload = Upload.new 
    end 

    def show 
    @upload = @parent.uploads.find(params[:id]) 
    @total_uploads = @parent.uploads.find(:all, :conditions =>{ :user_id => @upload.user.id}) 
end 

def create 
    @upload = @parent.uploads.build(params[:upload]) 
    @upload.document_content_type = MIME::Types.type_for(@upload.document.original_filename).to_s 
    @upload.document = params[:upload] 
     if @upload.save 
    flash[:notice] = "sucessfully saved upload" 
    respond_with{redirect_to [@parent, :uploads]} 
    respond_with{ render :json => {:upload => polymorphic_upload_path(@parent)} } 
    else 
    render :action => 'new' 
    end 
end 

views/uploads/index.html.erb 
<% unless @uploads.blank? %> 
    <% @uploads.each do |upload| %> 
    <%= render :partial => 'upload', :locals => {:collection => 
    @upload.try(:document)} %> 
    <% end %> 
<% end %> 
<div id="uploads"> 
    <h3>Upload a document</h3> 
    <%= render 'form', :parent => @parent, :upload => @upload.new % 
    </div> 

:

예상대로 작동처럼
Also i have tried to fix this double render error by modifying the create action call to render or redirect without success. This is an excerpt from the log. 
Render and/or redirect were called multiple times in this action 
controllers/uploads_controller.rb:37:in `create' 

감사

+1

정확히 무엇이 잘못 되었나요? 부분을 ​​렌더링하는 루프가 있고 기본 인덱스가 렌더링됩니다. 꼭해야 할 일을하는 것처럼 보입니다. 덕분에 –

답변

2

모든 보인다. 여러 개의 렌더링을 보는 이유는 부분적으로 "uploads/_upload.html.erb"부분을 여러 번 렌더링하기 때문입니다. 각 사용자 업로드마다 하나씩 렌더링됩니다.

http://localhost:3000/users/1/uploads으로의 리디렉션을 원하십니까? 지금은 사용자가 많은 업로드를하고 있다고 말했으므로이 URL은 UploadsController의 색인 작업 인 id 1의 사용자에 속한 업로드에 매핑됩니다.

중첩 된 리소스는 범위에있는 리소스와 같습니다. 이 경우 귀하의 업로드 범위는 자신이 소속 된 사용자에게 적용됩니다.

+0

. views/users/show.html.erb로 리다이렉트하기를 원한다. 왜냐하면 어떤 이유로 index.html.erb에서 호출되는 폼 부분은 절대 폼을로드하지 않고 둘째로 이미지를 표시하지 않기 때문에 대신 내가보기/사용자/show.erb.html로 리다이렉트한다고 생각했다. 이 때문에입니다 – brg

+0

또한 오류를 제공 respond_with {redirect_to [@ parent : : uploads] respond_with {render : json => {: upload => polymorphic_upload_path (@parent)}} json을 리디렉션하고 렌더링 할 수 없습니다. 난 당신이 response_to 블록에서 할 수있을 것 같아요, 당신은 js 및 HTML 요청을 구별하실 수 있습니다. – brg

+0

다수의 리디렉션 '생성'과 렌더링 : 37 : 컨트롤러/uploads_controller.rb에서 여러 번 호출했다 렌더링 및/또는 재 : 감사 –