Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sorry spotted this is bit late ,Ok now looking at your form code which look something like this </p> <pre><code>&lt;%= form_for @photo, :html =&gt; {:multipart =&gt; true} do |f| %&gt; &lt;% if @photo.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@photo.errors.count, "error") %&gt; prohibited this photo from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @photo.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;fieldset&gt; &lt;legend&gt;Upload a Photo&lt;/legend&gt; &lt;div class="field"&gt; &lt;%= f.file_field :image %&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;%= f.submit "Upload Photo", :class =&gt; "btn btn-small" %&gt; &lt;% end %&gt; </code></pre> <p>Now there this is not a problem with <code>rails</code> or <code>carrierwave</code> or <code>strong_parameter</code> it is something how <code>html</code> works . It like this, if you have <code>file</code> input and if <code>nothing</code> is attached to it the <code>name</code> and <code>value</code> pair is not sent to the server by <code>HTML</code> think of it something like <code>checkbox</code> or <code>disabled</code> field </p> <p>Now since your form only contain <code>&lt;%= f.file_field :image %&gt;</code> and it does not contain any other field (attributes of photo model)</p> <p>therefore the <code>photo</code> hash would not get constructed when the <code>file</code> input <code>does not</code> contain any attachment</p> <p>which is evident in your log as well </p> <h3>Parameter (without attachment attached) =></h3> <pre><code>{"utf8"=&gt;"✓", "authenticity_token"=&gt;"IvOieBvxdA6qzVKrt1dYBuCXlt+uuWCyWjUAuTK0XEU=", "commit"=&gt;"Upload Photo"} </code></pre> <h3>Parameter (with attachment attached) =></h3> <pre><code> {"utf8"=&gt;"✓", "authenticity_token"=&gt;"I4O0w+Wk8nJaD6HJRSC+FfuAip5NVE6TkCUFDEN+sW0=", "photo"=&gt;{"image"=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x007fc730b5de08 @tempfile=#&lt;Tempfile:/var/folders/0m/n32lgww55vzf5pfc4kh17gm00000gr/T/RackMultipart20130801-32602-1nj6u2b&gt;, @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"&gt;}, "commit"=&gt;"Upload Photo"} </code></pre> <p>now you can see the difference between the two and that the difference is the reason of your error because in </p> <p><strong>1st case</strong> </p> <p><code>params[:photo].present?</code> => false</p> <p><strong>2nd case</strong> </p> <p><code>params[:photo].present?</code> => true </p> <p>and hence when you do this <code>params.require(:photo).permit(:image)</code> the code throws the errors </p> <p>because of the line mention that <code>.require(:photo)</code> which is <code>not</code> present in <code>params</code></p> <h3>Solution:-</h3> <p>Perhaps you can do something like this </p> <pre><code> def photo_params if params[:photo].present? params.require(:photo).permit(:image) end end </code></pre> <h3>Conclusion :-</h3> <p>It not anyone fault because that is how the <code>HTML</code> work if <code>no</code> attachment <code>no</code> <code>name=value</code> pair submitted to server and because of which <code>photo</code> params is not sent to server hence the hash is does not have them and hence strong parameter throws the error</p> <p>Hope this help </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload