When downloading the repository on the machine, I gave the following commands:
$ rails generate migration RemoveRecipe_ *typeToRecipe recipe_* type:string
$ rails db:migrate RAILS *ENV=test*
*$ rails generate model recipe_* type name:string
$ rails db:migrate RAILS_ENV=test
I modified the recipe_type.rb and createrecipe_type.rb files as mentioned below and passed the same rails db: migrate mentioned.
These are the main ruby on rails project files:
routes.rb:
Rails.application.routes.draw do
root to: 'recipes#index'
resources :recipes
resources :recipe_types
end
recipe_type.rb:
class RecipeType < ApplicationRecord
has_many :recipes
end
recipe.rb:
class Recipe < ApplicationRecord
validates :title, :recipe_type, :cuisine, :difficulty, :cook_time,
:ingredients, :cook_method, presence: true
belongs_to :recipe_type
def cook_time_min
"#{cook_time} minutos"
end
end
create_recipe.rb:
class CreateRecipes < ActiveRecord::Migration[5.2]
create_table :recipe_types do |t|
t.string :name
t.timestamps
end
def change
create_table :recipes do |t|
t.string :title
t.string :recipe_type
t.string :cuisine
t.string :difficulty
t.integer :cook_time
t.belongs_to :recipe_type
t.timestamps
end
end
end
create_recipe_type.rb:
class CreateRecipeTypes < ActiveRecord::Migration[5.2]
def change
create_table :recipe_types do |t|
t.string :name
t.timestamps
end
create_table :recipes do |t|
t.string :title
t.string :recipe_type
t.string :cuisine
t.string :difficulty
t.integer :cook_time
t.belongs_to :recipe_type
t.datetime :published_at
t.timestamps
end
end
end
recipes_controllers.rb:
class RecipesController < ApplicationController
def index
@recipes = Recipe.all
end
def show
@recipe = Recipe.find(params[:id])
end
def new
@recipe = Recipe.new
end
def create
@recipe = Recipe.new(recipe_params)
if @recipe.save
redirect_to @recipe
else
flash[:alert] = 'Você deve informar todos os dados da receita'
render :new
end
end
def edit
@recipe = Recipe.find(params[:id])
end
def update
@recipe = Recipe.find(params[:id])
if @recipe.update(recipe_params)
redirect_to @recipe
else
flash[:alert] = 'Você deve informar todos os dados da receita'
render :edit
end
end
private
def recipe_params
params.require(:recipe).permit(:title, :recipe_type, :cuisine, :difficulty,
:cook_time, :ingredients, :cook_method)
end
end
index.html.erb:
<h1>CookBook</h1>
<p>Bem-vindo ao maior livro de receitas online</p>
<%= link_to 'Enviar uma receita', new_recipe_path %>
<% @recipes.each do |recipe| %>
<h1><%= link_to recipe.title, recipe %></h1>
<ul>
<li><%= recipe.recipe_type %></li>
<li><%= recipe.cuisine %></li>
<li><%= recipe.difficulty %></li>
<li><%= recipe.cook_time_min %></li>
</ul>
<% end %>
I pass the following rspec command specifically on visitor_visit_homepage_spec.rb line 11:
:~/workspace/cookbook_parte7$ rspec ./spec/features/visitor_visit_homepage_spec.rb:11
Run options: include {:locations=>{"./spec/features/visitor_visit_homepage_spec.rb"=>[11]}}
Visitor visit homepage
and view recipe (FAILED - 1)
Failures:
1) Visitor visit homepage and view recipe
Failure/Error:
recipe = Recipe.create(title: 'Bolo de cenoura', difficulty: 'Médio',
recipe_type: recipe_type, cuisine: 'Brasileira',
cook_time: 50,
ingredients: 'Farinha, açucar, cenoura',
cook_method: 'Cozinhe a cenoura, corte em pedaços pequenos, misture com o restante dos ingredientes')
ActiveModel::MissingAttributeError:
can't write unknown attribute `recipe_type_id`
# ./spec/features/visitor_visit_homepage_spec.rb:14:in `block (2 levels) in <top (required)>'
Finished in 0.04808 seconds (files took 1.72 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/features/visitor_visit_homepage_spec.rb:11 # Visitor visit homepage and view recipe
And after making changes to the recipe.rb and create_recipe.rb files, I ran the following command again:
$ rails db:migrate RAILS_ENV=test
I don’t understand why the error, relationships between the tables are not made?
I am participating in Locaweb’s selection process, and there are 4 challenges left to complete the delivery of the challenges and the deadline is 28/10/2019.
I look forward to a comment, tip or suggestion.
Thanks,