Rectangle 27 0

Rails 4 strong parameters param not found error with carrierwave?


fetch
params.fetch(:photo, {})
params[:photo]
require

Note a second argument:

Excellent -- this is exactly what I was looking for. I don't see why this isn't the best practice advertised.

Note
Rectangle 27 0

Rails 4 strong parameters param not found error with carrierwave?


def photo_params
     if params[:photo].present?
      params.require(:photo).permit(:image)
     end
   end
{"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"}
<%= 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 %>
params.require(:photo).permit(:image)
params[:photo].present?
{"utf8"=>"", "authenticity_token"=>"IvOieBvxdA6qzVKrt1dYBuCXlt+uuWCyWjUAuTK0XEU=", "commit"=>"Upload Photo"}

@Kirka121 Sorry man I seriously don't remember much on this but just a question how come photo_params will be false

It not anyone fault because that is how the HTML work if no attachment no name=value pair submitted to server and because of which photo params is not sent to server hence the hash is does not have them and hence strong parameter throws the error

Now since your form only contain <%= f.file_field :image %> and it does not contain any other field (attributes of photo model)

Now there this is not a problem with rails or carrierwave or strong_parameter it is something how html works . It like this, if you have file input and if nothing is attached to it the name and value pair is not sent to the server by HTML think of it something like checkbox or disabled field

Perhaps you can do something like this

This is very helpful and gets at the heart of the problem, but I think I need to accept Mrjaco12's answer as a more canonical approach since it's documented in the rails guides. Thanks for your help though!

because of the line mention that .require(:photo) which is not present in params

now you can see the difference between the two and that the difference is the reason of your error because in

therefore the photo hash would not get constructed when the file input does not contain any attachment

what if we need "if photo_params[:image].present?" in this case photo_params is false, and [:image] hash is nil. so we cant check for present?

which is evident in your log as well

Note