繁体   English   中英

参数数目错误(给定1,预期为0)

[英]wrong number of arguments (given 1, expected 0)

我想将照片上传到我的网站。 我选择照片并单击“添加照片”后,出现此错误。 有什么想法我可以解决这个问题吗? 在此处输入图片说明

photos_controller.rb

class PhotosController < ApplicationController

def create
@wall = Wall.find(params[:wall_id])

if params [:images]
  params[:images].each do |img|
    @wall.photos.create(image: img)
  end

  @photos = @wall.photos
  redirect_back(fallback_location: request.referer, notice: "Saved...")

 end

end
end

wall.controller.rb

class WallsController < ApplicationController
  before_action :set_wall, except: [:index, :new, :create]
  before_action :authenticate_user!, except: [:show]
  before_action :is_authorised, only: [:listing, :pricing, :description, :photo_upload, :location, :update]

  def index
    @walls = current_user.walls
  end

  def new
    @wall = current_user.walls.build
  end

  def create
    @wall = current_user.walls.build(wall_params)
    if @wall.save
      redirect_to listing_wall_path(@wall), notice: "Saved..."
    else
      fash[:alert] = "Something went wrong..."
      render :new
    end
  end


  def photo_upload
    @photos = @wall.photos
  end

  def location
  end

  def update
    if @wall.update(wall_params)
      flash[:notice] = "Saved..."
    else
      flash[:notice] = "Something went wrong..."
    end
    redirect_back(fallback_location: request.referer)
  end

  private

    def set_wall
      @wall = Wall.find(params[:id])
    end

    def is_authorised
      redirect_to root_path, alert: "You don't have permission" unless current_user.id == @wall.user_id
    end

    def wall_params
      params.require(:wall).permit(:size_sqm, :visibility, :traffic, :wall_name, :summary, :address, :price)
    end

end

在Rails控制器操作中, params是一个Hash值,并按以下方式获取Hash值:

params[:name]

在您的代码中, params[:images]之间有多余的空格,这意味着转换为通过名称params调用方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM