我正在尝试创建一个表单,用户可以在其中编辑现有视频标题和说明。发送 PATCH 请求时,出现以下错误。 StackOverflow 上的另一篇文章说,如果发送字符串而不是对象,则会导致错误,但当我控制台记录它时,我看到一个对象。有什么想法吗?
视频控制器:
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
编辑容器
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);
编辑表格
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)
const { video_id, ...rest } = this.state
this.props.updateVideo(video_id, { video: { ...rest } })
}
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;