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

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

问题描述

我在使用carrierwave 和rails 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 的强参数要求存在图像参数才能提交表单,但由于用户尚未选择图像,因此不存在.

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.

有没有办法用强参数来做到这一点?

Is there a way to do this with strong parameters?

这是我尝试在未选择照片的情况下使用表单时的开发日志: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

其他相关文件:* 模型/photo.rb - https://gist.github.com/leemcalilly/95c54a5df4e4ee6518da* 控制器/photos_controller.rb - https://gist.github.com/leemcalilly/c0723e6dc5478b0f914d* uploaders/image_uploader.rb - https://gist.github.com/leemcalilly/5e43f6524734991773ae* 视图/照片/index.html.erb - https://gist.github.com/leemcalilly/a8c4c808e5e8a802933b* 视图/照片/_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 提供了解决方案.以与 require 方法相同的方式使用 fetch 方法.注意第二个参数:它提供默认值.因此,如果找不到 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 强参数参数未找到载波错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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