找不到Rails 4强参数param的载波错误 [英] Rails 4 strong parameters param not found error with carrierwave

查看:81
本文介绍了找不到Rails 4强参数param的载波错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了载波和轨线4个强参数的问题。我有一个带有载波上传按钮的非常简单的模型。如果有人提交上传表单而未选择要上传的文件,我想显示一条错误消息。

I'm having trouble with carrierwave and rails 4 strong parameters. I have a very simple model with a carrier wave upload button. I'd like to show an error message if someone submits the upload form without choosing a file to upload.

现在,我收到一个 param not found:photo 错误,并显示以下消息:

Right now, I get a param not found:photo error with this message:

  # Never trust parameters from the scary internet, only allow the white list through.
    def photo_params
      params.require(:photo).permit(:image)
    end

之所以发生此错误,是因为Rails 4的强大参数要求必须存在image参数才能提交表单,但由于用户尚未选择图像,所以该参数不存在。

This error is happening because Rails 4's strong parameters is requiring that the image parameter be present to submit the form, but it's not there because the user hasn't selected an image.

我找不到解决此问题的方法,无法将其重定向到同一操作并显示错误消息。

I can't figure out a way to work around this and have it redirect to the same action and display an error message.

这里是我尝试使用未选择照片的表单时的开发日志:
https://gist.github.com/leemcalilly/09b6166ce1af6ca6c833

Here's the development log when I try to use the form without a photo selected: https://gist.github.com/leemcalilly/09b6166ce1af6ca6c833

这是我选择照片并将其成功上传时的开发日志:
https://gist.github.com/leemcalilly/1f1e584f56aef15e7af1

And here's the development log when I choose a photo and it uploads successfully: https://gist.github.com/leemcalilly/1f1e584f56aef15e7af1

其他r相关文件:
* models / photo.rb- https://gist.github.com/leemcalilly/95c54a5df4e4ee6518da
* controllers / photos_controller.rb- https://gist.github.com/leemcalilly/c0723e6dc5478b0f914d
* uploaders / image_uploader.rb- https://gist.github.com/leemcalilly/5e43f6524734991773ae
* views / photos / index.html.erb- https://gist.github.com/leemcalilly/a8c4c808e5e8a802933b
* views / photos / _form.html.erb- https://gist.github.com/leemcalilly/cd0fd518c1b47d9bfb62
* initializers / carrierwaver.rb- https://gist.github.com/leemcalilly/49e04fa1dda891dd108b

Other relevant files: * models/photo.rb - https://gist.github.com/leemcalilly/95c54a5df4e4ee6518da * controllers/photos_controller.rb - https://gist.github.com/leemcalilly/c0723e6dc5478b0f914d * uploaders/image_uploader.rb - https://gist.github.com/leemcalilly/5e43f6524734991773ae * views/photos/index.html.erb - https://gist.github.com/leemcalilly/a8c4c808e5e8a802933b * views/photos/_form.html.erb - https://gist.github.com/leemcalilly/cd0fd518c1b47d9bfb62 * initializers/carrierwaver.rb - https://gist.github.com/leemcalilly/49e04fa1dda891dd108b

推荐答案

rails guides提供解决方案。使用 fetch 方法与 require 方法相同。 请注意第二个参数:它提供默认值。因此,如果找不到 params [:photo] ,则默认值(一个空哈希,如以下示例所示)为返回:

The rails guides provides the solution. Use the fetch method in the same way as the require method. Note a second argument: it supplies the default value. So, if the params[:photo] would not be found, the default value (an empty hash, as of the example below) will be returned:

params.fetch(:photo, {})

这篇关于找不到Rails 4强参数param的载波错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆