开发者

Rails-3 routing issue

I have an UploadsController and UsersController where Users' has_many :uploads' via polymorphic attachment in uploads.rb.

Now when i navigate to http://localhost:3000/users/1/uploads

I get re-routed to Uploads#index and rendered is called multiple times as shown below:

Started GET "/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

This my config/routes

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{ ren开发者_运维知识库der :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>

Edit:

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' 

Thanks


Everything looks like it's working as expected. The reason you see multiple renders is because it's rendering the partial "uploads/_upload.html.erb" multiple times, one for each user upload.

Where do you want http://localhost:3000/users/1/uploads to redirect to? As it is now, you've said that a User has many uploads, so this url maps to the uploads belonging to the User with id 1, that is the index action of the UploadsController.

A nested resource is like a resource that lives in a scope. In this case, your uploads are scoped to the user they belong to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜