Home>

I'm trying to make a simple Tweet app.
I used scaffled when creating the posting function.
I want to make a list of individual posts for users
I want to add @ current_user.id (the ID of the person who posted) to the create method of the upload controller.

It may be difficult to convey with the first question.
I would appreciate it if you could answer.

It seems that the number of values ​​sent and the number of values ​​returned are different, but I don't know what to do.

ArgumentError in UploadsController # create
wrong number of arguments (given 2, expected 0..1)
Corresponding source code
class UploadsController</pre>
<strong>from viwe</strong>
<pre><code><% = form_with (model: upload, local: true) do | form |%>
<% if upload.errors.any?%>
    
     <% = pluralize (upload.errors.count, "error")%>prohibited this upload from being saved: 
    <ul>
    <% upload.errors.full_messages.each do | message |%>
      <li><% = message%></li>
    <% end%>
    </ul>
    
<% end%>
  
  <% = form.label: image%>
  <% = form.file_field: image%>
  
  
  <% = form.label: Name%>
  <% = form.text_field: title%>
  
  
  <% = form.label: Price%>
  <% = form.text_field: body%>
  
  
  <% = form.submit%>
  
<% end%>

Please describe here what you have tried for the problem.
from<% = form_with (model: upload, local: true) do | form |%>to<% = form_tag>
I described it withand described the reception of the create method as Upload.new (title: params [: title]), but it didn't work.

Supplementary information (FW/tool version, etc.)

Ruby 2.5.7
Rails 5.2.3
PostgreSQL

  • Answer # 1

    I thought it was appropriate to add current_user.id to upload_params while doing the following here

    @upload = Upload.new (upload_params)
    def upload_params
      params.require (: upload) .permit (: image,: title,: body) .merge (user_id: @ current_user.id)
    end

    I'm adding @ current_user.id to upload_params with merge (I don't know where @current_user is defined, but I borrowed it from the question)
    This is an old article, but this may be helpful for merge.
    In the merge method, save the information that you have in another table in advance together with the entered params.