我有一個UploadsController和UsersController,其中用戶' has_many:uploads'通過uploads.rb中的多態附件。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
這是我的config /路線
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'
感謝
究竟是什麼錯誤?你有一個渲染一個局部的循環,然後是主索引渲染......它似乎在做它應該是的。 – 2011-05-06 22:41:53