ruby-on-rails-3 - Rails3 中回形针编辑操作的问题

标签 ruby-on-rails-3 paperclip

所以我运行了一个常规的生成脚手架并使用库存表单来处理上传。

所有上传都运行良好,并且图像附加得非常完美。我遇到的问题是当我转到“编辑”并尝试更改图像时,这是我收到的错误:

Routing Error

No route matches "/uploads"

这是我的 Controller 的样子。名称是“uploads_controller.rb”

class UploadsController < ApplicationController
  # GET /uploads
  # GET /uploads.xml
  def index
    @uploads = Upload.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @uploads }
    end
  end

  # GET /uploads/1
  # GET /uploads/1.xml
  def show
    @upload = Upload.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @upload }
    end
  end

  # GET /uploads/new
  # GET /uploads/new.xml
  def new
    @upload = Upload.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @upload }
    end
  end

  # GET /uploads/1/edit
  def edit
    @upload = Upload.find(params[:id])
  end

  # POST /uploads
  # POST /uploads.xml
  def create
    @upload = Upload.new(params[:upload])

    respond_to do |format|
      if @upload.save
        format.html { redirect_to(@upload, :notice => 'Upload was successfully created.') }
        format.xml  { render :xml => @upload, :status => :created, :location => @upload }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @upload.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /uploads/1
  # PUT /uploads/1.xml
  def update
    @upload = Upload.find(params[:id])

    respond_to do |format|
      if @upload.update_attributes(params[:upload])
        format.html { redirect_to(@upload, :notice => 'Upload was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @upload.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /uploads/1
  # DELETE /uploads/1.xml
  def destroy
    @upload = Upload.find(params[:id])
    @upload.destroy

    respond_to do |format|
      format.html { redirect_to(uploads_url) }
      format.xml  { head :ok }
    end
  end
end

“upload.rb”模型文件如下所示:

class Upload < ActiveRecord::Base
    has_attached_file :image
end

show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <b>Name:</b>
  <%= @upload.name %>
</p>

<p>
  <b>Description:</b>
  <%= @upload.description %>
</p>

<p>
  <b>Your Image:</b>
  <%= image_tag @upload.image.url %>
</p>

<%= link_to 'Edit', edit_upload_path(@upload) %> |
<%= link_to 'Back', uploads_path %>

编辑.html.erb

<h1>Editing upload</h1>

<%= render 'form' %>

<%= link_to 'Show', @upload %> |
<%= link_to 'Back', uploads_path %>

_form.html.erb

<%= form_for (@upload), :url => uploads_path, :html => { :multipart => true } do |f| %>
  <% if @upload.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@upload.errors.count, "error") %> prohibited this upload from being saved:</h2>

      <ul>
      <% @upload.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </div>
  <div class="field">
    <%= f.file_field :image %>
  </div>  
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

谢谢。

编辑:“rake paths”输出的相关部分:

 uploads GET    /uploads(.:format)          {:action=>"index", :controller=>"uploads"}
        uploads POST   /uploads(.:format)          {:action=>"create", :controller=>"uploads"}
     new_upload GET    /uploads/new(.:format)      {:action=>"new", :controller=>"uploads"}
    edit_upload GET    /uploads/:id/edit(.:format) {:action=>"edit", :controller=>"uploads"}
         upload GET    /uploads/:id(.:format)      {:action=>"show", :controller=>"uploads"}
         upload PUT    /uploads/:id(.:format)      {:action=>"update", :controller=>"uploads"}
         upload DELETE /uploads/:id(.:format)      {:action=>"destroy", :controller=>"uploads"}

最佳答案

看来错误出在我的 _form 部分中。

我定义了一个 :url 属性,但我不应该这样做。

Paperclip 需要更新其安装说明以反射(reflect) Rails 3 的更改。

irc.freenode.net 上#RubyOnRails 中的出色人员帮助我解决了这个问题。

关于ruby-on-rails-3 - Rails3 中回形针编辑操作的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4162053/

相关文章:

ruby-on-rails-3 - VCR 仅运行一次用于使用 selenium webkit 在 capybara 中进行集成测试

ruby-on-rails - 使用 Rails 进行移动地理定位?

ruby-on-rails - 回形针 - 从 Amazon S3 中删除文件?

javascript - 使用 AJAX 从 jQuery 向 Rails 服务器提交表单在 IE11 中不起作用

css - 如何使用回形针 gem 制作圆形缩略图?

ruby-on-rails - Ruby on Rails - 回形针不保存到数据库

ruby-on-rails - 使用 Paperclip 从图片中获取 GPS 元数据

ruby-on-rails - Nginx 在连接到 Unicorn 时获得权限被拒绝

ruby-on-rails - 添加 Accepts_nested_attributes_for 时,Fields_for 消失

ruby-on-rails-3 - Rails的路线:如何在重定向中传递 “all”请求参数