Closed adriennedao closed 7 years ago
can I see the create action from RecipesController ?
class RecipesController < ApplicationController
def new
@recipe = Recipe.new
end
def show
@recipe = Recipe.find(params[:id])
end
def index
@recipes = Recipe.all
end
def create
@recipe = Recipe.create!(recipe_params)
redirect_to @recipe
end
def update
@recipe = Recipe.find(params[:id])
@recipe.update(recipe_params)
redirect_to recipe_path(@recipe)
end
def edit
@recipe = Recipe.find(params[:id])
end
def destroy
@recipe = Recipe.find(params[:id])
@recipe.destroy
redirect_to recipes_path
end
private
def recipe_params
params.require(:recipe).permit(:name, :img_url)
end
end
In your create method, try adding the current user:
def create
@recipe = Recipe.create!(recipe_params.merge(user: @current_user))
redirect_to @recipe
end
Thank you Jesse! Though it is still unresolved! More error "can't write unknown attribute user_id
"
ok great! this is saying that a recipe does not belong to a user. Do you have a migration that adds a user_id to recipes table? if not you can create one:
$ rails g migration add_users_to_recipes user:references
Migrations are pending , should I run this: run: bin/rails db:migrate RAILS_ENV=development
yes!
It resolved, thanks again!
excellent! feel free to open another issue!
I tried fixed the error "validation failed, user does not exist" I have checked on resources and checked the models relationship. Functionality of CRUD was working earlier, after having added the user, and session, it created the error. Unable to add. Error appeared . I expected to happen. happened instead. My repo link is and my question is about lines .