Rails 4强参数param not found with carrierwave

Lee*_*lly 19 ruby-on-rails carrierwave strong-parameters ruby-on-rails-4

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

现在,我收到param not found:photo此消息的错误:

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

发生此错误是因为Rails 4的强参数要求提供图像参数以提交表单,但由于用户未选择图像,因此不存在.

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

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

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

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

其他相关文件:*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

小智 53

轨道引导提供了解决方案.以与fetch方法相同的方式使用该require方法.请注意第二个参数:它提供默认值.因此,如果params[:photo]找不到,将返回默认值(空哈希,如下例所示):

params.fetch(:photo, {})
Run Code Online (Sandbox Code Playgroud)


Vir*_*ren 12

对不起发现这有点晚了,好了,现在看看你的表单代码看起来像这样

<%= form_for @photo, :html => {:multipart => true}  do |f| %>
  <% if @photo.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@photo.errors.count, "error") %> prohibited this photo from being saved:</h2>

      <ul>
      <% @photo.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <fieldset>
    <legend>Upload a Photo</legend>
    <div class="field">
      <%= f.file_field :image %>
    </div>              
   </fieldset>  

   <%= f.submit "Upload Photo", :class => "btn btn-small" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

现在有这不是一个问题rails或者carrierwave或者strong_parameter是一些东西是如何html工作的.像这样,如果你有file输入,如果nothing连接到它namevalue一对不被发送到服务器HTML,想想它像checkboxdisabled

现在,因为您的表单只包含 <%= f.file_field :image %> ,它不包含任何其他字段(照片模型的属性)

因此,photofile输入does not包含任何附件时,不会构造散列

这在您的日志中也很明显

参数(未附加附件)=>

{"utf8"=>"?", "authenticity_token"=>"IvOieBvxdA6qzVKrt1dYBuCXlt+uuWCyWjUAuTK0XEU=", "commit"=>"Upload Photo"}
Run Code Online (Sandbox Code Playgroud)

参数(附带附件)=>

 {"utf8"=>"?", "authenticity_token"=>"I4O0w+Wk8nJaD6HJRSC+FfuAip5NVE6TkCUFDEN+sW0=", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fc730b5de08 @tempfile=#<Tempfile:/var/folders/0m/n32lgww55vzf5pfc4kh17gm00000gr/T/RackMultipart20130801-32602-1nj6u2b>, @original_filename="bag.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"bag.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Upload Photo"}
Run Code Online (Sandbox Code Playgroud)

现在你可以看到两者之间的差异,而差异是你的错误的原因,因为在

第一个案例

params[:photo].present? =>假

第二个案例

params[:photo].present? =>是的

因此,当您执行此操作时params.require(:photo).permit(:image),代码会抛出错误

由于线路的提及,.require(:photo)这是not存在于params

解:-

也许你可以做这样的事情

   def photo_params
     if params[:photo].present?
      params.require(:photo).permit(:image)
     end
   end
Run Code Online (Sandbox Code Playgroud)

结论: -

这不是任何人的错,因为这是HTML工作如果no附件no name=value对提交到服务器的工作,因为其中photo没有发送到服务器,因此哈希是没有它们因此强参数抛出错误

希望这有帮助