【问题标题】:Rails-3 routing issueRails-3 路由问题
【发布时间】:2011-05-06 22:36:17
【问题描述】:

我有一个 UploadsController 和 UsersController,其中用户的 has_many :uploads' 通过 uploads.rb 中的多态附件。

现在当我导航到http://localhost:3000/users/1/uploads

我被重新路由到 Uploads#index 并多次调用渲染 如下图:

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

这是我的配置/路线

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' 

谢谢

【问题讨论】:

  • 到底出了什么问题?你有一个渲染部分的循环,然后渲染主索引......它似乎正在做它应该做的事情。

标签: routing routes url-routing ruby-on-rails-3


【解决方案1】:

一切看起来都按预期工作。您看到多次渲染的原因是因为它多次渲染部分“uploads/_upload.html.erb”,每个用户上传一次。

您希望http://localhost:3000/users/1/uploads 重定向到哪里?就像现在一样,你说一个用户有很多上传,所以这个 url 映射到属于 id 1 的用户的上传,也就是 UploadsController 的索引操作。

嵌套资源就像存在于范围内的资源。在这种情况下,您的上传内容仅限于他们所属的用户。

【讨论】:

  • 谢谢。我希望它重定向到views/users/show.html.erb,因为由于某些原因,在index.html.erb中调用的表单部分从不加载表单,其次它不显示图像,所以我以为我确实将其重定向到views/users/show.erb.html。谢谢
  • 它也给出了错误:在控制器/uploads_controller.rb:37:in `create'中多次调用渲染和/或重定向
  • 多次重定向和渲染是因为这个:respond_with{redirect_to [@parent, :uploads]} respond_with{ render :json => {:upload => polymorphic_upload_path(@parent)} } 你可以'不重定向并渲染 json。我认为您可能希望在 respond_to 块中执行此操作,这将允许您区分 js 和 html 请求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2011-05-24
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
相关资源
最近更新 更多