Ruby on rails——错误的参数数量(1)[rails]

时间:2022-08-03 23:20:34

I've just change my database from sqlite3 to postgresql in my Ruby on rails.

我刚刚在Ruby on rails中将数据库从sqlite3更改为postgresql。

Every things worked before but now I have a "wrong number of arguments (2 for 1)" when I submit the form. I don't understand why !

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

Here my code :

这里我的代码:

my photo_controller file :

我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

my photo.rb file :

我的照片。rb文件:

class Photo < ActiveRecord::Base
has_attached_file :image
end

my new.html.erb file ( here is the form and after I chose the image and submit it, I have the error ) :

我的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 %>

my show.html.erb file :

我的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 %>

Backtrace:

回溯:

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'

Please help me :)

请帮我:)

2 个解决方案

#1


2  

Looks like you're seeing this error and you should just upgrade Paperclip to the latest version, 3.5.2.

看起来您看到了这个错误,您应该将Paperclip升级到最新版本3.5.2。

You're running a pretty old paperclip anyway.

反正你用的是一个很旧的回形针。

#2


0  

Just pulling out the essential pieces from above:

只要从上面拉出最重要的部分:

Error:

错误:

wrong number of arguments (2 for 1)

Source:

来源:

Controller:

控制器:

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

Model:

模型:

class Photo < ActiveRecord::Base
  has_attached_file :image
end

View:

观点:

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

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

Stacktrace:

加:

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'

#1


2  

Looks like you're seeing this error and you should just upgrade Paperclip to the latest version, 3.5.2.

看起来您看到了这个错误,您应该将Paperclip升级到最新版本3.5.2。

You're running a pretty old paperclip anyway.

反正你用的是一个很旧的回形针。

#2


0  

Just pulling out the essential pieces from above:

只要从上面拉出最重要的部分:

Error:

错误:

wrong number of arguments (2 for 1)

Source:

来源:

Controller:

控制器:

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

Model:

模型:

class Photo < ActiveRecord::Base
  has_attached_file :image
end

View:

观点:

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

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

Stacktrace:

加:

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'