【问题标题】:editable form ActiveRecord::RecordNotFound (Couldn't find Video with 'id'=undefined)可编辑表单 ActiveRecord::RecordNotFound(找不到带有 'id'=undefined 的视频)
【发布时间】:2020-10-27 17:46:19
【问题描述】:

我正在尝试创建一个表单,用户可以在其中编辑现有的视频标题和说明。发送 PATCH 请求时,出现以下错误。参数正在读取正确的视频 ID,但在错误的位置。我需要修改什么才能让它工作?

编辑表格

class Edit extends React.Component {

    constructor(props) {
        super(props)
        this.state = {
            video_id: this.props.match.params.id,
            video_title: "",
            video_description: "",
        }
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    update(field) {
        return (e) => {
            this.setState({
                [field]: e.currentTarget.value
            })
        }
    }   

    handleSubmit() {
        // debugger
        this.props.updateVideo(this.state.video_id)
    }

    render() {
        return (
            <div className="edit-container">
                <form className="edit-form" onSubmit={this.handleSubmit}>
                    <label>
                        <input 
                            type="text"
                            value={this.state.video_title}
                            placeholder="Your video's title"
                            onChange={this.update("video_title")}
                        />
                        <div>{this.state.video_title}</div>
                    </label>
                    <label>
                        <input 
                            type="textarea"
                            value={this.state.video_description}
                            placeholder="Your video's description"
                            onChange={this.update("video_description")}
                        />  
                    </label>
                    <button>Edit</button>
                </form>
            </div>
        );
    }
}

export default Edit;

编辑容器

import { connect } from  'react-redux';
import Edit from './edit';
import { fetchVideo, updateVideo } from '../../actions/video_actions';

const mSTP = ( state, ownProps ) => {
    
    return {
        // video: state.entities.videos[state.session.id],
        video: state.entities.videos[ownProps.match.params.id],
        // errors: state.errors
    }
};

const mDTP = dispatch => ({
    fetchVideo: videoId => dispatch(fetchVideo(videoId)),
    updateVideo: video => dispatch(updateVideo(video)),
});

export default connect(mSTP, mDTP)(Edit);

视频 api 工具

export const updateVideo = video => {
    return $.ajax({
        method: 'patch',
        url: `api/videos/${video.id}`,
        data: { video }
    })
}

视频控制器

   def update
        @video = Video.find(params[:id])
        if @video.update(video_params)
            render :show
        else
            render json: @video.errors.full_messages, status: 422
        end
    end

  
    def video_params 
        params.require(:video).permit(:video_title, :video_description, :owner_id, :video_url)
    end
Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

  root to: 'static_pages#root'

  namespace :api, defaults: {format: :json} do
    resources :users, only: [:create, :index]
    resource :session, only: [:create, :destroy]
    resources :videos
  end

end

【问题讨论】:

    标签: javascript html jquery ruby-on-rails react-redux


    【解决方案1】:

    在 handleSubmit 中,您将 video_id 传递给 updateVideo 函数:

    handleSubmit() {
        // debugger
        this.props.updateVideo(this.state.video_id)
    }
    

    但您的 updateVideo 函数需要一个视频对象:

    export const updateVideo = video => {
        return $.ajax({
            method: 'patch',
            url: `api/videos/${video.id}`,
            data: { video }
        })
    }
    

    所以你从video.id 得到未定义。

    【讨论】:

    • 我将 handleSubmit 更改为 this.props.updateVideo(this.props.video) 所以它是一个对象。在提交时它会回滚。请看附件截图
    • 这与 ActiveStorage 有关。如果您不尝试更新模型中的图像,那么我会删除 video_url 参数以查看是否有效。
    • 如果我从 video_params 中删除 url,上传将停止正常工作并且视频也没有更新
    • 更新您的问题或为您的新问题创建一个新问题可能值得。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多