ruby-on-rails - 参数数量错误(给定 1,预期为 0)
问题描述
我想上传照片到我的网站。在我选择照片并单击“添加照片”后,出现此错误。有什么想法可以解决这个问题吗?
照片控制器.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
推荐阅读
- javascript - 如何从循环中的变量中减去变量
- reactjs - React-Bootstrap 需要 npm install bootstrap 和 bootstrap CDN 还是其中之一?
- kubernetes - Airflow 无法将日志写入 s3 (v1.10.9)
- php - Foreach 循环仅将数组中的最后一个值添加到表中
- r - 在 R 中,向量和数据帧被认为是有序集还是无序集?
- authentication - 发布/订阅请求身份验证作为服务(或通过服务密钥)而不是 Auth0 临时密钥
- google-apps-script - 忽略空单元格并跳过 Google 表格/日历同步中的重复条目
- c# - 如何打印数组?
- chart.js - Chart.js 仅绘制 y 轴直到点
- swift - 如何检查一个字符串是否包含另一个字符串但字符可以变化?