[Rails] How to resolve wrong number of arguments (given 2, expected 0..1) of user_id

Premise

Introducing devise to link users and posts (It is not necessary to devise, but current_user must be defined by @current_user)

What i did

Added ** by referring to here

@gear = Gear.new(
  **gear_params,
  user_id: current_user.id
)

⇒ Sinked by the mysterious error hash key" "is not a Symbol

I wrote everything without using the gear_params method

@gear = Gear.new(
      category: params[:category],
      name: params[:name],
      maker: params[:maker],
      price: params[:price],
      memo: params[:memo],
      user_id: current_user.id
      )

For some reason it is rolled back and not saved in the database. .. ʻActiveRecord :: RecordInvalid (validation failed: enter User` (unsolved)

Solution

If you write it completely separately, you don't have to think about hashes as symbols.

def create
    @gear = Gear.new(
      gear_params
      )
    @gear.user_id = current_user.id
      .
      .
      .
end

The site that I used as a reference

Because I stumbled upon saving a post from the model associated with the user. As a memorandum

Supplement

gear_params writes:

  private
  def gear_params
    params.require(:gear).permit(:category, :name, :maker, :price, :memo)
  end

Recommended Posts

[Rails] How to resolve wrong number of arguments (given 2, expected 0..1) of user_id
wrong number of arguments (given 1, expected 0)
ArgumentError (wrong number of arguments (given 0, expected 1)) error message
[Rails] Sign-up function using devise error number of arguments (given 0, expected 1)
What to do if you get an Argument Error: wrong number of arguments (given 2, expected 0) in your RSpec test
Summary of how to write annotation arguments
How to determine the number of parallels
[Rails] Introduction of pry-rails ~ How to debug binding.pry
How to use JQuery in js.erb of Rails6
What to do if you get a wrong number of arguments error in binding.pry
How to write Rails
[Rails, JS] How to implement asynchronous display of comments
[Rails] How to change the column name of the table
How to uninstall Rails
[Rails] How to get the contents of strong parameters
Summary of how to implement default arguments in Java
[Rails] How to display the list of posts by category
[Rails] How to write user_id (foreign key) in strong parameter
How to resolve Sprockets :: DoubleLinkError
[rails] How to post images
How to resolve Git conflicts
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
How to use rails join
How to number (number) with html.erb
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
[Rails] How to get the URL of the transition source and redirect
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Rails] How to omit the display of the character string of the link_to method
[Rails] How to change the page title of the browser for each page
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Rails] PG :: DuplicateTable: ERROR: relation "users" How to resolve already exists
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[rails] How to display db information
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
How to use setDefaultCloseOperation () of JFrame
[Rails] How to add new pages
Rails on Tiles (how to write)