Ethan Franson Ethan Franson - 6 months ago 10
Ruby Question

I can't get my new to do list item to post.

I have been trying to follow the steps to get my do make a to do list. Everything has been going smoothly until I submit my item. I keep getting a error saying "ArgumentError in ItemsController#create, wrong number of arguments (0 for 1)". It highlights the second line under my create method saying thats where the problem is. My code in my controllers look like so.

class UsersController < ApplicationController
def show
@user = params[:id].blank? ? current_user : User.find(params[:id])
@items = @user.items
end
end

class ItemsController < ApplicationController
def create
@user = User.find(params[:user_id])
@items = @user.items.create(params.require[:items].permit(:name))
redirect_to @user
end
end


I am not sure why it's giving me a arguments error. I have been following a video, and theirs is working, so i'm stumped. Also I don't know if you need to see my db migrate but here it is:

class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|
t.string :name
t.references :user, index: true, foreign_key: true

t.timestamps null: false
end
end


end
If anyone could help me out I would be very much appreciated!

Answer

ArgumentError in ItemsController#create, wrong number of arguments (0 for 1)

As I aid, it should be

@items = @user.items.create(params.require(:item).permit(:name))

Also, to go by with Rails way, you can call it by a private method like below

#items_controller

class ItemsController < ApplicationController
  def create
    @user = User.find(params[:user_id])
    @items = @user.items.create(item_params)
    redirect_to @user
  end

  private
  def item_params
    @items = @user.items.create(params.require(:item).permit(:name))
  end
end