https://ujk-ujk.blogspot.com/2025/04/rails-7-denemeler-5.html
İletişim sayfası
Uygulamamıza İletişim sayfası ekleyerek devam edeceğiz. Öncelikle test rutinlerini ekleyelim.
test/controllers/static_pages_controller_test.rb
require "test_helper"
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
....
test "should get contact" do
get static_pages_contact_url
assert_response :success
assert_select "title", "İletişim | Yeni App"
end
end
Tabi ki test yaparsak hata verecektir.
$ rails t
Aynı daha önce about eylemi için yaptıklarımızı tekrarlayacağız. Öncelikle yönlendirmesini yapalım.
config/routes.rb
Rails.application.routes.draw do
get "static_pages/home"
get "static_pages/help"
get "static_pages/about"
get "static_pages/contact"
....
end
Şimdi de kontrolöre eylemi ekleyelim.
app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
Şimdi bir de görsel sayfası ekleyelim.
app/views/static_pages/contact.html.erb
<% provide(:title, 'İletişim') %>
<h1>İletişim</h1>
<p>
Yeni App uygulamamız için iletişimi konunun anlatıldığı
<a href="https://ujk-ujk.blogspot.com/2025/03/rails-7-denemeler-1.html">
blog sayfalarında</a> yorum yaparak bulabilirsiniz.
</p>
Artık uygulamamız testten geçiyor durumda. İletişim sayfası eklendi.
Rails yönlendirmeler
İsimlendirilmiş yönlendirmeler için tabi ki uygulamamızın yönlendirmelerinin yapıldığı config/routes.rb dosyası kullanılır.
root "static_pages#home"
Bu satır uygulamamızın / ana root adresini static_pages#home eylemine yönlendiriyor. Bu şekil yönlendirmenin bir önemli etkisi daha var. Yönlendirmeleri düz URL değeri yerine isimlendirilmiş yönlendirmelerle yapmak. Uygulamamızda kullanabileceğimiz iki isimlendirilmiş yönlendirme var, root_path ve root_url değişkenleri.
root_path -> '/'
root_url -> 'http://www.example.com/'
path değişken uygulama root'una göre bağıl, url değişken ise tam url web adresi olur. Normalde redirect hariç path değişkenler kullanacağız.
Şimdi diğer sayfalar için de isimlendirilmiş yönlendirme yapmak için routes.rb dosyamızı değiştirelim.
get "static_pages/help"
bunun yerine
get "/help", to: "static_pages#help"
yapmalıyız. Bu sayede help_path ve help_url değişkenleri oluşur. Hepsi için uygularsak
config/routes.rb
Rails.application.routes.draw do
root "static_pages#home"
get "/help", to: "static_pages#help"
get "/about", to: "static_pages#about"
get "/contact", to: "static_pages#contact"
.....
Yönlendirmeler değiştiği için testlerimiz hata verecektir. Onları da bir düzenleyelim.
static_pages_controller_test.rb
require "test_helper"
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get root_path
assert_response :success
assert_select "title", "Yeni App"
end
test "should get help" do
get help_path
assert_response :success
assert_select "title", "Yardım | Yeni App"
end
test "should get about" do
get about_path
assert_response :success
assert_select "title", "Hakkımızda | Yeni App"
end
test "should get contact" do
get contact_path
assert_response :success
assert_select "title", "İletişim | Yeni App"
end
end
İsimlendirilmiş yönlendirmeleri kullanalım