Ruby problem z template

0

Witam,

jestem w trakcie nauki ruby i podczas robienia projektu z yt mam problem:

Template is missing
Missing template pages/places/index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in: * "C:/Users/tomas/desktop/frontend/ruby/publicinternet/app/views" * "C:/Users/tomas/.local/share/gem/ruby/3.2.0/gems/turbo-rails-1.4.0/app/views" * "C:/Users/tomas/.local/share/gem/ruby/3.2.0/gems/actiontext-7.0.6/app/views" * "C:/Users/tomas/.local/share/gem/ruby/3.2.0/gems/actionmailbox-7.0.6/app/views"
Extracted source (around line #3):
1
2
3
4
5
6

class PlacesController < ApplicationController
    def index
      render "pages/places/index"
    end
  end

index.html.erb :

<div>
<h1>List of places to search</h1>
</div>

routes.rb:

Rails.application.routes.draw do
  root "places#index"
end

mój path do index:

app\views\pages\places\index.html.erb

taki kod działa i wyświetla plik no ale to nie jest jako template:

class PlacesController < ApplicationController
    def index
        render file: 'C:/Users/tomas/desktop/frontend/ruby/publicinternet/app/views/pages/places/index.html.erb'
      end
end

dziwne jest to że przy uzyć file znajduję plik a przy samym render już nie. W czym leży problem ?

0

Z tego co pokazałeś, teoretycznie powinno działać.
Możliwe, że masz coś popsute w configu lub może to być problem z którymś gemem.
Błąd ewidentnie wskazuje na to, że w podanym pathu nie ma tego templata.

Spróbuj wygenerować nowego kontrolera i zobacz czy błąd nadal występuje:

rails generate controller Some index

Następnie wchodzisz w link <adres_twojej_strony>/some/index

0

Ten sam problem.
Stworzyłem projekt za pomocą komendy:

rails new my-rails-app --database=sqlite3

Następnie:

rails db:create
rails db:migrate

Gemfile - nic w nim nie zmieniałem robiłem tak samo jak w filmie na yt:

source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.2.2"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.6"

# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"

# Use sqlite3 as the database for Active Record
gem "sqlite3", "~> 1.4"

# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"

# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"

# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"

# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"

# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

# Use Sass to process CSS
# gem "sassc-rails"

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

group :development, :test do
  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
  gem "debug", platforms: %i[ mri mingw x64_mingw ]
end

group :development do
  # Use console on exceptions pages [https://github.com/rails/web-console]
  gem "web-console"

  # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
  # gem "rack-mini-profiler"

  # Speed up commands on slow machines / big apps [https://github.com/rails/spring]
  # gem "spring"
end

group :test do
  # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
  gem "capybara"
  gem "selenium-webdriver"
  gem "webdrivers"
end


0
  1. Napisz po kolei jakie komendy wywołujesz. Nie widać w jaki sposób tworzysz encję kontrolera.
  2. W jaki sposób serwujesz tą aplikację? Przez serwer developerski? rails server ? Jeśli tak to wklej występujący błąd z tej konsoli.

Jaki to tutorial ?

0

Otwieram powershell. Nawiguję do folderu w którym chcę utworzyć projekt. Używam komendy:

rails new publicinternet --database=sqlite3

Następnie wpisuję 2 komendy jak na filmiku:

rails db:create

rails db:migrate

Później w routes.rb:

Rails.application.routes.draw do
  root "places#index"
end

Następnie tworzę controller - places_controller.rb:

class PlacesController < ApplicationController
    def index
        render 'pages/places/index'
      end     
end

W views tworzę 2 foldery pages a w nim places i w places plik index.html.erb:

<div>
<h1>List of places to search</h1>
</div>

Uruchamiam server wpisując w konsoli:
rails s

Wchodzę na localhost:3000 i tam już pojawia się bład template is missing

Template is missing
Missing template pages/places/index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in: * "C:/Users/tomas/desktop/frontend/ruby/publicinternet/app/views" * "C:/Users/tomas/.local/share/gem/ruby/3.2.0/gems/turbo-rails-1.4.0/app/views" * "C:/Users/tomas/.local/share/gem/ruby/3.2.0/gems/actiontext-7.0.6/app/views" * "C:/Users/tomas/.local/share/gem/ruby/3.2.0/gems/actionmailbox-7.0.6/app/views"
Extracted source (around line #3):
1
2
3
4
5
              
class PlacesController < ApplicationController
    def index
        render 'pages/places/index'
      end     
end

Rails.root: C:/Users/tomas/desktop/frontend/ruby/publicinternet

Tekst z konsoli:

Started GET "/" for 127.0.0.1 at 2023-07-13 21:24:10 +0200
  ActiveRecord::SchemaMigration Pluck (0.4ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by PlacesController#index as HTML
Completed 500 Internal Server Error in 63ms (ActiveRecord: 0.0ms | Allocations: 3463)



ActionView::MissingTemplate (Missing template pages/places/index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}.

Searched in:
  * "C:/Users/tomas/desktop/frontend/ruby/publicinternet/app/views"
  * "C:/Users/tomas/.local/share/gem/ruby/3.2.0/gems/turbo-rails-1.4.0/app/views"
  * "C:/Users/tomas/.local/share/gem/ruby/3.2.0/gems/actiontext-7.0.6/app/views"
  * "C:/Users/tomas/.local/share/gem/ruby/3.2.0/gems/actionmailbox-7.0.6/app/views"
):

app/controllers/places_controller.rb:3:in `index'


0
  1. Stwórz nowy projekt rails new publicinternet --database=sqlite3
  2. Stwórz nowego kontrolera rails generate controller Places index
  3. Pozmieniaj pliki routers.rb,places_controller.rb,intex.html.erb wedle uznania.

Staraj się korzystać z generatorów jeśli są dostępne i wystarczające.
Prawdopodobnie coś źle wpisałeś lub źle umieściłeś.

0

Zrobiłem jak napisałeś stworzyłem nowy projekt itd.

places_controller.rb:

class PlacesController < ApplicationController
  def index
    render 'places/index'
  end
end


routes.rb:

Rails.application.routes.draw do
  get 'places/index'
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
  root "places#index"
end

Zmieniłem tylko te 2 pliki dopisałem root w routes i render w controllerze i dalej ten sam błąd.

Nie wiem co jest nie tak.

ruby -v
ruby 3.2.2

rails -v
Rails 7.0.6

0

A jeśli usuniesz to co wprowadziłeś render oraz root, następnie odpalisz rails s i wejdziesz u siebie na <adres_strony>/places/index, to błąd nadal występuje?

0

http://localhost:3000/places/index

No template for interactive request

PlacesController#index is missing a template for request formats: text/html

NOTE!
Unless told otherwise, Rails expects an action to render a template with the same name,
contained in a folder named after its controller. If this controller is an API responding with 204 (No Content),
which does not require a template, then this error will occur when trying to access it via browser,
since we expect an HTML template to be rendered for such requests. If that's the case, carry on.

Takie coś.

index.html.erb został wygenerowany automatycznie wygląda tak:

<h1>Places#index</h1>
<p>Find me in app/views/places/index.html.erb</p>


0

To dość dziwne.
W tej aplikacji zrób:

  1. rails generate controller Some index
  2. Nic nie zmieniając wejdź na <adres_strony>/some/index

Problem nadal występuje?
Jeśli tak to problem leży po stronie samego railsa, gemów, rubiego. Być może trzeba przeinstalować.

0

Ten sam błąd:

No template for interactive request
SomeController#index is missing a template for request formats: text/html
NOTE!
Unless told otherwise, Rails expects an action to render a template with the same name,
contained in a folder named after its controller. If this controller is an API responding with 204 (No Content),
which does not require a template, then this error will occur when trying to access it via browser,
since we expect an HTML template to be rendered for such requests. If that's the case, carry on.

Mogę wrzucić projekt na github i zobaczysz czy u ciebie wyskakuję ten sam problem ?

0

Rzeczywiście po uruchomeniu serwera w cmd, działa !
W takim razie powershell oraz git bash jest problemem.

Dziękuję za pomoc jesteś wielki ! :D

0
AtomBari22 napisał(a):

To super, dziwne jest to że tylko wszystko dobrze działa na cmd

Możesz sprawdzić debuggerem, lecz z góry ostrzegam, że może to nie być, prosta przeprawa.
https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem

Obsługa podobna do gdb, więc jak miałeś styczność to powinno pójść gładko.
Prawdopodobnie Powershell, inaczej interpretuje pewne rzeczy niż standardowy cmd.

Do windowsa mam awersję jeśli chodzi o pracę na nim.
Ten system służy mi tylko do kompilowania i różnych testów, więc więcej nie pomogę :P

1 użytkowników online, w tym zalogowanych: 0, gości: 1