jQuery文件上传v4 for Rails 3,路由错误

时间:2012-10-18 07:58:32

标签: ruby-on-rails

我跟着这个tutorial并遇到了困难。

通过调用http://localhost:3000/home我收到路由错误 也许我只是在调用错误的页面。 如果有人帮助我,我会很感激。


Hier是我的路线.rb:

Try::Application.routes.draw do
 resources :uploads
 root :to => "home#index"
end


uploads_controller.rb:

class UploadsController < ApplicationController
  def home
  end

  def index
  end


  def create
    @upload = Upload.new(params[:upload])
    if @upload.save
      render :json => { :pic_path => @upload.picture.url.to_s , :name => @upload.picture.instance.attributes["picture_file_name"] }, :content_type => 'text/html'
    else
      render :json => { :result => 'error'}, :content_type => 'text/html'
    end
  end

end

和views / home / index.html.erb:

<%= stylesheet_link_tag('jquery.fileupload-ui') %>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<%= javascript_include_tag 'jquery.fileupload', 'jquery.fileupload-ui' %>

<script type="text/javascript" charset="utf-8"> 
   $(function () {
  $('.upload').fileUploadUI({
        uploadTable: $('.upload_files'),
        downloadTable: $('.download_files'),
        buildUploadRow: function (files, index) {
            var file = files[index];
            return $('<tr><td>' + file.name + '<\/td>' +
                    '<td class="file_upload_progress"><div><\/div><\/td>' +
                    '<td class="file_upload_cancel">' +
                    '<button class="ui-state-default ui-corner-all" title="Cancel">' +
                    '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
                    '<\/button><\/td><\/tr>');
        },
        buildDownloadRow: function (file) {
            return $('<tr><td><img alt="Photo" width="40" height="40" src="' + file.pic_path + '">' + file.name + '<\/td><\/tr>');
        },
    });
});
</script>

<div class="files"> 
<%= form_for @upload, :html => { :class => "upload", :multipart => true } do |f| %>
  <%= f.file_field :picture %>
  <div>Upload files</div>
<% end %>

<table class="upload_files"></table>
<table class="download_files"></table>
</div>

home_controller.rb:

class HomeController < ApplicationController
def index
  @upload  = Upload.new
end
end

2 个答案:

答案 0 :(得分:1)

你的路径文件是说根路径,即'/'将转到home #index。

'/ home'没有路由建议你添加:

match '/home' => 'uploads#home'

答案 1 :(得分:-1)

当你说root:to =&gt;路径文件中的“home #index”表示家庭控制器索引操作

您的视图文件结构也需要修改为views / controller_name / index.html.erb: