wrong number of arguments (given 1, expected 0)

核能气质少年 提交于 2021-02-11 14:49:59

问题


I want to upload photos to my website. After i selected the photos and click on "Add Photos" this error comes up. Any ideas how i can solve this?

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

walls.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

回答1:


In a Rails controller action, params is a Hash value and a Hash value is fetched like below:

params[:name]

In your code you're having an unwanted space between params and [:images] which means translates to calling a method by name params



来源:https://stackoverflow.com/questions/50843008/wrong-number-of-arguments-given-1-expected-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!