Add words controller with index and show actions
This commit is contained in:
parent
ad08b283b0
commit
6202a6e778
10
app/controllers/words_controller.rb
Normal file
10
app/controllers/words_controller.rb
Normal file
@ -0,0 +1,10 @@
|
||||
class WordsController < ApplicationController
|
||||
|
||||
def index
|
||||
@words = Word.all
|
||||
end
|
||||
|
||||
def show
|
||||
@word = Word.find_by_id(params[:id])
|
||||
end
|
||||
end
|
2
app/helpers/word_helper.rb
Normal file
2
app/helpers/word_helper.rb
Normal file
@ -0,0 +1,2 @@
|
||||
module WordHelper
|
||||
end
|
12
app/views/application/_definitions.html.erb
Normal file
12
app/views/application/_definitions.html.erb
Normal file
@ -0,0 +1,12 @@
|
||||
<table>
|
||||
<tr>
|
||||
<td><b>Part of Speech</b></td>
|
||||
<td><b>Definition</b></td>
|
||||
</tr>
|
||||
<% word.definitions.each do |definition| %>
|
||||
<tr>
|
||||
<td><b><%= definition.pos %></b></td>
|
||||
<td><%= definition.definition %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
@ -19,7 +19,8 @@
|
||||
<h1>Words</h1>
|
||||
<% @words.each do |word| %>
|
||||
<h2><%= word.word %></h2>
|
||||
|
||||
<%= render "application/definitions", word: word %>
|
||||
<!--
|
||||
<table>
|
||||
<tr>
|
||||
<td><b>Part of Speech</b></td>
|
||||
@ -32,4 +33,5 @@
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
-->
|
||||
<% end %>
|
||||
|
3
app/views/words/index.html.erb
Normal file
3
app/views/words/index.html.erb
Normal file
@ -0,0 +1,3 @@
|
||||
<% @words.each do |word| %>
|
||||
<p><%= link_to word.word, word %></p>
|
||||
<% end %>
|
3
app/views/words/show.html.erb
Normal file
3
app/views/words/show.html.erb
Normal file
@ -0,0 +1,3 @@
|
||||
<h1><%= @word.word %></h1>
|
||||
|
||||
<%= render "application/definitions", word: @word %>
|
@ -2,6 +2,7 @@ Rails.application.routes.draw do
|
||||
devise_for :users
|
||||
#get 'dictionary/index'
|
||||
resources :dictionary, only: [:index]
|
||||
resources :words, only: [:index, :show]
|
||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||
|
||||
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
|
||||
|
22
spec/controllers/words_spec.rb
Normal file
22
spec/controllers/words_spec.rb
Normal file
@ -0,0 +1,22 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe WordsController, type: :controller do
|
||||
describe "GET :index" do
|
||||
it 'renders the index template' do
|
||||
get :index
|
||||
expect(response).to render_template(:index)
|
||||
end
|
||||
|
||||
it 'sets @words instance variable' do
|
||||
FactoryBot.create(:word)
|
||||
get :index
|
||||
expect(assigns(:words)).to be_a(ActiveRecord::Relation)
|
||||
end
|
||||
|
||||
it 'fetches all words into @words' do
|
||||
5.times { FactoryBot.create(:word) }
|
||||
get :index
|
||||
expect(assigns(:words).count).to eq(5)
|
||||
end
|
||||
end
|
||||
end
|
15
spec/helpers/word_helper_spec.rb
Normal file
15
spec/helpers/word_helper_spec.rb
Normal file
@ -0,0 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
# Specs in this file have access to a helper object that includes
|
||||
# the WordHelper. For example:
|
||||
#
|
||||
# describe WordHelper do
|
||||
# describe "string concat" do
|
||||
# it "concats two strings with spaces" do
|
||||
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
RSpec.describe WordHelper, type: :helper do
|
||||
#pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
39
spec/requests/word_spec.rb
Normal file
39
spec/requests/word_spec.rb
Normal file
@ -0,0 +1,39 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "Words", type: :request do
|
||||
describe "GET /index" do
|
||||
|
||||
it "renders the index template" do
|
||||
get "/words"
|
||||
expect(response).to render_template(:index)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /show/:val" do
|
||||
|
||||
before(:each) do
|
||||
@word = FactoryBot.create(:word)
|
||||
end
|
||||
|
||||
it "renders the show template" do
|
||||
get "/words/#{@word.id}"
|
||||
expect(response).to render_template(:show)
|
||||
end
|
||||
|
||||
it "sets @word instance variable" do
|
||||
get "/words/#{@word.id}"
|
||||
expect(assigns(:word)).to be_a(Word)
|
||||
end
|
||||
|
||||
it "displays the word name" do
|
||||
get "/words/#{@word.id}"
|
||||
expect(response.body).to include(@word.word)
|
||||
end
|
||||
|
||||
it "displays the definitions" do
|
||||
@definition = FactoryBot.create(:definition)
|
||||
get "/words/#{@definition.word_id}"
|
||||
expect(response.body).to include(@definition.definition)
|
||||
end
|
||||
end
|
||||
end
|
11
spec/requests/words_spec.rb
Normal file
11
spec/requests/words_spec.rb
Normal file
@ -0,0 +1,11 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "Words", type: :request do
|
||||
describe "GET /index" do
|
||||
|
||||
it "renders the index template" do
|
||||
get "/words"
|
||||
expect(response).to render_template(:index)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user