2013-11-28 57 views
3

我只是将我的数据库从sqlite3更改为我的Ruby on rails的postgresql。红宝石轨道 - 错误的参数数量(2为1)[Rails]

以前的每件事情都有效,但现在我有一个“错误数量的参数(2为1)”,当我提交表单。我不明白为什么!

这里我的代码:

我photo_controller文件:

class PhotosController < ApplicationController 
    before_action :set_photo, only: [:show, :edit, :update, :destroy] 

    # GET /photos 
    # GET /photos.json 
    def index 
    @photos = Photo.all 
    end 

    # GET /photos/1 
    # GET /photos/1.json 
    def show 
    end 

    # GET /photos/new 
    def new 
    @photo = Photo.new 
    end 

    # GET /photos/1/edit 
    def edit 
    end 

    # POST /photos 
    # POST /photos.json 
    def create 
    @photo = Photo.new(photo_params) 

    respond_to do |format| 
     if @photo.save 
     format.html { redirect_to @photo, notice: 'Photo was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @photo } 
     else 
     format.html { render action: 'new' } 
     format.json { render json: @photo.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /photos/1 
    # PATCH/PUT /photos/1.json 
    def update 
    respond_to do |format| 
     if @photo.update(photo_params) 
     format.html { redirect_to @photo, notice: 'Photo was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @photo.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /photos/1 
    # DELETE /photos/1.json 
    def destroy 
    @photo.destroy 
    respond_to do |format| 
     format.html { redirect_to photos_url } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_photo 
     @photo = Photo.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def photo_params 
     params.require(:photo).permit(:image) 
    end 
end 

我photo.rb文件:

class Photo < ActiveRecord::Base 
has_attached_file :image 
end 

我new.html.erb文件(这里是形式和后我选择了图像并提交,我有错误):

<h1>New photo</h1> 


<%= form_for @photo, :html => { :multipart => true } do |f| %> 
    <div class="field"> 
    <%= f.file_field :image %> 
    </div> 

    <div class="action"> 
    <%= f.submit %> 
    </div> 


<% end %> 

我show.html.erb文件:

<h1> Je viens de faire un upload ! </h1> 



<% if @photo.image? %> 

<!-- Load Feather code --> 
<script type="text/javascript" src="http://feather.aviary.com/js/feather.js"></script> 

<!-- Instantiate Feather --> 
<script type='text/javascript'> 
    var featherEditor = new Aviary.Feather({ 
     apiKey: ' f20374413e3ff5a8', 
     apiVersion: 3, 
     theme: 'light', // Check out our new 'light' and 'dark' themes! 
     tools: 'all', 
     appendTo: '', 
     onSave: function(imageID, newURL) { 
      var img = document.getElementById(imageID); 
      img.src = newURL; 
     }, 
     onError: function(errorObj) { 
      alert(errorObj.message); 
     } 
    }); 
    function launchEditor(id, src) { 
     featherEditor.launch({ 
      image: id, 
      url: src 
     }); 
     return false; 
    } 
</script> 

<div id='injection_site'></div> 

<img id='image1' src='<%= @photo.image.url %>'/> 

<!-- Add an edit button, passing the HTML id of the image and the public URL of the image --> 
<p><input type='image' src='http://images.aviary.com/images/edit-photo.png' value='Edit photo' onclick="return launchEditor('image1', '<%= @photo.image.url %>');" /></p> 
<% end %> 

回溯:

Application trace : 
app/controllers/photos_controller.rb:27:in `create' 

Full trace : 
activesupport (4.0.0) lib/active_support/callbacks.rb:78:in `run_callbacks' 
paperclip (3.0.4) lib/paperclip/callbacks.rb:26:in `run_paperclip_callbacks' 
paperclip (3.0.4) lib/paperclip/attachment.rb:382:in `post_process' 
paperclip (3.0.4) lib/paperclip/attachment.rb:106:in `assign' 
paperclip (3.0.4) lib/paperclip.rb:194:in `block in has_attached_file' 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in `public_send' 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in `_assign_attribute' 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:29:in `block in assign_attributes' 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:23:in `each' 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:23:in `assign_attributes' 
activerecord (4.0.0) lib/active_record/core.rb:192:in `initialize' 
activerecord (4.0.0) lib/active_record/inheritance.rb:27:in `new' 
activerecord (4.0.0) lib/active_record/inheritance.rb:27:in `new' 
app/controllers/photos_controller.rb:27:in `create' 
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action' 
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action' 
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action' 
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action' 
activesupport (4.0.0) lib/active_support/callbacks.rb:413:in `_run__921501253494556441__process_action__callbacks' 
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' 
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action' 
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action' 
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action' 
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument' 
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument' 
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action' 
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action' 
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action' 
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process' 
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process' 
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch' 
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch' 
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action' 
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call' 
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch' 
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call' 
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call' 
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each' 
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call' 
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call' 
rack (1.5.2) lib/rack/etag.rb:23:in `call' 
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call' 
rack (1.5.2) lib/rack/head.rb:11:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call' 
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context' 
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call' 
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call' 
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call' 
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' 
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__2330035833412540959__call__callbacks' 
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' 
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app' 
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call' 
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged' 
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged' 
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged' 
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' 
rack (1.5.2) lib/rack/runtime.rb:17:in `call' 
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call' 
rack (1.5.2) lib/rack/lock.rb:17:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call' 
railties (4.0.0) lib/rails/engine.rb:511:in `call' 
railties (4.0.0) lib/rails/application.rb:97:in `call' 
rack (1.5.2) lib/rack/lock.rb:17:in `call' 
rack (1.5.2) lib/rack/content_length.rb:14:in `call' 
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service' 
/home/ubuntu/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service' 
/home/ubuntu/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run' 
/home/ubuntu/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread' 

请帮我:)

+3

无法在没有堆栈跟踪的情况下找出错误。 –

+0

好吧,我把它添加到问题 – Rabbit56

+0

哪一行是'app/controllers/photos_controller.rb:27:'创建'' –

回答

2

看起来像你看到this error,你应该只是回形针升级到最新版本,3.5.2

无论如何,您正在运行一个非常古老的回形针。

+0

非常感谢,就是这样!我跟着一个老教程,我不知道为什么有时它有效,有时不是! – Rabbit56

0

刚刚拉出从上面的基本部分:

错误:

wrong number of arguments (2 for 1) 

来源:

控制器:

1: class PhotosController < ApplicationController 
... 
24: # POST /photos 
25: # POST /photos.json 
26: def create 
27: @photo = Photo.new(photo_params) 

型号:

class Photo < ActiveRecord::Base 
    has_attached_file :image 
end 

查看:

<%= form_for @photo, :html => { :multipart => true } do |f| %> 
    <div class="field"> 
    <%= f.file_field :image %> 
    </div> 

    <div class="action"> 
    <%= f.submit %> 
    </div>  
<% end %> 

堆栈跟踪:

Application trace : 
app/controllers/photos_controller.rb:27:in `create' 

Full trace : 
activesupport (4.0.0) lib/active_support/callbacks.rb:78:in `run_callbacks' 
paperclip (3.0.4) lib/paperclip/callbacks.rb:26:in `run_paperclip_callbacks' 
paperclip (3.0.4) lib/paperclip/attachment.rb:382:in `post_process' 
paperclip (3.0.4) lib/paperclip/attachment.rb:106:in `assign' 
paperclip (3.0.4) lib/paperclip.rb:194:in `block in has_attached_file' 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in `public_send' 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in `_assign_attribute' 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:29:in `block in assign_attributes' 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:23:in `each' 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:23:in `assign_attributes' 
activerecord (4.0.0) lib/active_record/core.rb:192:in `initialize' 
activerecord (4.0.0) lib/active_record/inheritance.rb:27:in `new' 
activerecord (4.0.0) lib/active_record/inheritance.rb:27:in `new' 
app/controllers/photos_controller.rb:27:in `create' 
+0

我不想编辑上面的帖子,因为这是一个相当宽松的重写。我确实相信这些是信息的重要部分。 –