Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

mikuzuhara

ActionView::Template::Error: undefined local variable or method `recipes'

Recommended Posts

Por favor, preciso de ajuda da comunidade Ruby. Sou recém chegado na linguagem Ruby e no framework Rails. Estou participando da Campus Code e não consigo progredir em um desafio proposto no treinamento. O repositório do projeto baixei em minha máquina virtual VMware com Ubuntu 18.04 e nele consta o projeto em Ruby on Rails e os testes para passar com rspec. Eis os arquivos sensíveis para concluir o projeto: 

 

home_controller.rb:

class HomeController < ApplicationController
  def page
      @recipes = Recipe.all
  end
end

page.html.erb:

<% if recipes == [] %>
     <h1>CookBook</h1>
     <p>Bem-vindo ao maior livro de receitas online</p>
<% else %>
    <% @recipes.each do |recipe| %>
    <h1><%= recipe.title %></h1>

    <li><%= recipe.recipe_type %></li>
    <li><%= recipe.cuisine %></li>
    <li><%= recipe.difficulty %></li>
    <li><%= recipe.cook_time %> minutos</li>
    <% end %>
<% end %>

recipe.rb:

class Recipe < ApplicationRecord
end

routes.rb:

Rails.application.routes.draw do
  root to: 'home#page'
  resources :recipe
end

 

E esse é o arquivo de testes visitor_visit_homepage_spec.rb:

 

require 'rails_helper'

feature 'Visitor visit homepage' do
  scenario 'successfully' do
    visit root_path

    expect(page).to have_css('h1', text: 'CookBook')
    expect(page).to have_css('p', text: 'Bem-vindo ao maior livro de receitas'\
                                        ' online')
  end

  scenario 'and view recipe' do
    #cria os dados necessários
    recipe = Recipe.create(title: 'Bolo de cenoura', recipe_type: 'Sobremesa',
                           cuisine: 'Brasileira', difficulty: 'Médio',
                           cook_time: 60)

    # simula a ação do usuário
    visit root_path

    # expectativas do usuário após a ação
    expect(page).to have_css('h1', text: recipe.title)
    expect(page).to have_css('li', text: recipe.recipe_type)
    expect(page).to have_css('li', text: recipe.cuisine)
    expect(page).to have_css('li', text: recipe.difficulty)
    expect(page).to have_css('li', text: "#{recipe.cook_time} minutos")
  end

  scenario 'and view recipes list' do
    #cria os dados necessários
    recipe = Recipe.create(title: 'Bolo de cenoura', recipe_type: 'Sobremesa',
                           cuisine: 'Brasileira', difficulty: 'Médio',
                           cook_time: 60)

    another_recipe = Recipe.create(title: 'Feijoada',
                                   recipe_type: 'Prato Principal',
                                   cuisine: 'Brasileira', difficulty: 'Difícil',
                                   cook_time: 90)

    # simula a ação do usuário
    visit root_path

    # expectativas do usuário após a ação
    expect(page).to have_css('h1', text: recipe.title)
    expect(page).to have_css('li', text: recipe.recipe_type)
    expect(page).to have_css('li', text: recipe.cuisine)
    expect(page).to have_css('li', text: recipe.difficulty)
    expect(page).to have_css('li', text: "#{recipe.cook_time} minutos")

    expect(page).to have_css('h1', text: another_recipe.title)
    expect(page).to have_css('li', text: another_recipe.recipe_type)
    expect(page).to have_css('li', text: another_recipe.cuisine)
    expect(page).to have_css('li', text: another_recipe.difficulty)
    expect(page).to have_css('li', text: "#{another_recipe.cook_time} minutos")
  end
end

Passo o comando rspec na pasta do projeto:

 

:~/workspace/cookbook_parte2$ rspec

Visitor visit homepage
  successfully (FAILED - 1)
  and view recipe (FAILED - 2)
  and view recipes list (FAILED - 3)

Recipe
  add some examples to (or delete) /home/massa-90/workspace/cookbook_parte2/spec/models/recipe_spec.rb (PENDING: Not yet implemented)

Pending: (Failures listed here are expected and do not affect your suite's status)

  1) Recipe add some examples to (or delete) /home/massa-90/workspace/cookbook_parte2/spec/models/recipe_spec.rb
     # Not yet implemented
     # ./spec/models/recipe_spec.rb:4


Failures:

  1) Visitor visit homepage successfully
     Failure/Error: <% if recipes == [] %>
     
     ActionView::Template::Error:
       undefined local variable or method `recipes' for #<#<Class:0x0000564c2a9f8630>:0x0000564c2a9f30b8>
       Did you mean?  @recipes
     # ./app/views/home/page.html.erb:1:in `_app_views_home_page_html_erb___526898541735595527_47442566303380'
     # ./spec/features/visitor_visit_homepage_spec.rb:5:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # NameError:
     #   undefined local variable or method `recipes' for #<#<Class:0x0000564c2a9f8630>:0x0000564c2a9f30b8>
     #   Did you mean?  @recipes
     #   ./app/views/home/page.html.erb:1:in `_app_views_home_page_html_erb___526898541735595527_47442566303380'

  2) Visitor visit homepage and view recipe
     Failure/Error: <% if recipes == [] %>
     
     ActionView::Template::Error:
       undefined local variable or method `recipes' for #<#<Class:0x0000564c2a9f8630>:0x0000564c2a68c0c8>
       Did you mean?  @recipes
     # ./app/views/home/page.html.erb:1:in `_app_views_home_page_html_erb___526898541735595527_47442566303380'
     # ./spec/features/visitor_visit_homepage_spec.rb:19:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # NameError:
     #   undefined local variable or method `recipes' for #<#<Class:0x0000564c2a9f8630>:0x0000564c2a68c0c8>
     #   Did you mean?  @recipes
     #   ./app/views/home/page.html.erb:1:in `_app_views_home_page_html_erb___526898541735595527_47442566303380'

  3) Visitor visit homepage and view recipes list
     Failure/Error: <% if recipes == [] %>
     
     ActionView::Template::Error:
       undefined local variable or method `recipes' for #<#<Class:0x0000564c2a9f8630>:0x0000564c2a4b2cc0>
       Did you mean?  @recipes
     # ./app/views/home/page.html.erb:1:in `_app_views_home_page_html_erb___526898541735595527_47442566303380'
     # ./spec/features/visitor_visit_homepage_spec.rb:41:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # NameError:
     #   undefined local variable or method `recipes' for #<#<Class:0x0000564c2a9f8630>:0x0000564c2a4b2cc0>
     #   Did you mean?  @recipes
     #   ./app/views/home/page.html.erb:1:in `_app_views_home_page_html_erb___526898541735595527_47442566303380'

Finished in 0.34634 seconds (files took 2.17 seconds to load)
4 examples, 3 failures, 1 pending

Failed examples:

rspec ./spec/features/visitor_visit_homepage_spec.rb:4 # Visitor visit homepage successfully
rspec ./spec/features/visitor_visit_homepage_spec.rb:12 # Visitor visit homepage and view recipe
rspec ./spec/features/visitor_visit_homepage_spec.rb:29 # Visitor visit homepage and view recipes list

E para criar a model recipe na linha do terminal e executar a migration criada em db/migrations:

 

$ rails generate model recipe title:string recipe_type:string cuisine:string difficulty:string cook_time:string
      invoke  active_record
      create    db/migrate/20191016221038_create_recipes.rb
      create    app/models/recipe.rb
      invoke    rspec
      create      spec/models/recipe_spec.rb
$ rspec
Migrations are pending. To resolve this issue, run:

        bin/rails db:migrate RAILS_ENV=test

$ cd bin
$ rails db:migrate RAILS_ENV=test== 20191016221038 CreateRecipes: migrating ====================================
-- create_table(:recipes)
   -> 0.0029s
== 20191016221038 CreateRecipes: migrated (0.0035s) ===========================

O que pode ser alterado nos códigos do projeto que eu não saiba?

 

Aguardo sua resposta.

 

Obrigado,

 

Marcelino

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.