ga-wdi-exercises / project1

[project] GA- Project 1
3 stars 75 forks source link

issue with "validation error"(adrienne) #274

Closed adriennedao closed 7 years ago

adriennedao commented 7 years ago

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 .

jshawl commented 7 years ago

can I see the create action from RecipesController ?

adriennedao commented 7 years ago
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
jshawl commented 7 years ago

In your create method, try adding the current user:

def create
    @recipe = Recipe.create!(recipe_params.merge(user: @current_user))
    redirect_to @recipe
end
adriennedao commented 7 years ago

Thank you Jesse! Though it is still unresolved! More error "can't write unknown attribute user_id"

jshawl commented 7 years ago

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
adriennedao commented 7 years ago

Migrations are pending , should I run this: run: bin/rails db:migrate RAILS_ENV=development

jshawl commented 7 years ago

yes!

adriennedao commented 7 years ago

It resolved, thanks again!

jshawl commented 7 years ago

excellent! feel free to open another issue!