ruby-on-rails ActionController::参数缺失(参数缺失或值为空:资产)

u4vypkhs  于 2023-01-22  发布在  Ruby
关注(0)|答案(1)|浏览(135)

我是一个编程新手,所以我很感激你的帮助!我已经在这方面停留了一段时间,但是当我试图从表单添加一条记录时,我的Rails服务器给了我ActionController::ParameterMissing(参数缺失或值为空:资产)。
这是我的资产管理员-

class Api::AssetsController < ApplicationController
  before_action :set_area
  before_action :set_asset, only: [:show, :update, :destroy]

  def index
    render json: @area.assets
  end

  def show
    render json: @asset
  end

  def create
    @asset = @area.assets.new(asset_params)
    if @asset.save
      render json: @asset
    else
      render json: {errors: @asset.errors }, status: :unprocessable_entity
    end
  end

  def update
    if @asset.update(asset_params)
      render json:@asset
    else
      render json: {errors: @asset.errors }, status: :unprocessable_entity
    end
  end

  def destroy
    @asset.destroy
    render json: {message: "Asset Deleted"}
  end

  private
  
    def set_area
      @area = Area.find(params[:area_id])
    end
    
    def set_asset
      @asset = @area.assets.find(params[:id])
    end
    
    def asset_params
      params.require(:asset).permit(:name, :description, :barcode, :status, :img )
    end
end

表格在这里-

import { useState } from 'react'
import {Form, Button} from 'react-bootstrap'
import {AssetConsumer} from '../../providers/AssetProvider'

const AssetForm = ({ setAdd, addAsset, areaId,}) => {
    const [asset, setAsset] = useState ({ name: '', description: '', barcode: '', status: '', img: ''})
    // const {areaId} = useParams

    const handleSubmit = (e) => {
        e.preventDefault()
        addAsset(areaId, asset)
        setAdd(false)
        setAsset({ name: '', description: '', barcode: '', status: '', img: ''})
    }

    return (
        <>
        <Form onSubmit={handleSubmit}>
      <Form.Group className="mb-3" >
        <Form.Label>Asset Image</Form.Label>
        <Form.Control  
            name='img'
            value={asset.img}
            onChange={(e) => setAsset({...asset, img: e.targetvalue})}
        />
      </Form.Group>
      <Form.Group className="mb-3" >
        <Form.Label>Name</Form.Label>
        <Form.Control  
            name='name'
            value={asset.name}
            onChange={(e) => setAsset({...asset, name: e.targetvalue})}
            required
        />
      </Form.Group>
      <Form.Group className="mb-3" >
        <Form.Label>Barcode</Form.Label>
        <Form.Control  
            name='barcode'
            value={asset.barcode}
            onChange={(e) => setAsset({...asset, barcode: e.targetvalue})}
          
        />
      </Form.Group>
      <Form.Group className="mb-3" >
        <Form.Label>Description</Form.Label>
        <Form.Control  
            name='description'
            value={asset.description}
            onChange={(e) => setAsset({...asset, description: e.targetvalue})}
            as="textarea"
            rows={3}
            
        />
      </Form.Group>
      <Form.Group className="mb-3" >
        <Form.Label>Status</Form.Label>
        <Form.Control  
            name='status'
            value={asset.status}
            onChange={(e) => setAsset({...asset, status: e.targetvalue})}
        />
      </Form.Group>

      <Button variant="primary" type="submit">
        Submit
      </Button>
    </Form>
        </>
    )
}

const ConnectedAssetForm = (props) => (
    <AssetConsumer>
        { value => <AssetForm {...value} {...props} />}
    </AssetConsumer>
)

export default ConnectedAssetForm;

这里是模型-

class Asset < ApplicationRecord
  belongs_to :area
  validates :name, :description, :barcode,:img,  presence: true
end
vsaztqbk

vsaztqbk1#

如下设置name属性name='asset[name]'

<Form.Control  
  name='asset[name]'
  value={asset.name}
  onChange={(e) => setAsset({...asset, name: e.targetvalue})}
  required
/>

相关问题