Read-only patch written, frontend shows dictionary in different languages
This commit is contained in:
@@ -3,6 +3,7 @@ class ActiveLanguageController < ApplicationController
|
||||
def set_active_language
|
||||
if Language.find_by_id(params[:active_language_id]) != nil
|
||||
cookies[:active_language_id] = params[:active_language_id]
|
||||
redirect_back fallback_location: root_path
|
||||
else
|
||||
redirect_to root_path, status: :unprocessable_entity
|
||||
end
|
||||
|
@@ -1,11 +1,19 @@
|
||||
class DictionaryController < ApplicationController
|
||||
def index
|
||||
@parts_of_speech = PartOfSpeech.all
|
||||
@parts_of_speech = PartOfSpeech.where(language_id: active_language)
|
||||
@words = Word.all
|
||||
end
|
||||
|
||||
def show
|
||||
@letter = params[:id]
|
||||
@words = Word.where('substr(word, 1, 1) = ?', @letter)
|
||||
@language = active_language
|
||||
|
||||
#@words.joins(:definitions).where(definitions: { language_id: active_language })
|
||||
end
|
||||
|
||||
private
|
||||
def active_language
|
||||
cookies[:active_language_id] || Language.where(name: "English")
|
||||
end
|
||||
end
|
||||
|
@@ -1,7 +1,8 @@
|
||||
module ActiveLanguageHelper
|
||||
|
||||
def active_language_select_tag
|
||||
languages = Language.all.to_a.delete_if {|language| language.name == "English" }
|
||||
#languages = Language.all.to_a.delete_if {|language| language.name == "English" }
|
||||
languages = Language.all.to_a
|
||||
options = options_from_collection_for_select(languages, "id", "name", cookies[:active_language_id] || 1)
|
||||
select_tag "active_language_id", options, { id: "active_language_select_tag" }
|
||||
end
|
||||
|
@@ -1,5 +1,5 @@
|
||||
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
|
||||
import "@hotwired/turbo-rails"
|
||||
//import "@hotwired/turbo-rails"
|
||||
import "controllers"
|
||||
|
||||
let switchLanguageForm = document.querySelector("#active_language_form");
|
||||
|
@@ -1,3 +1,7 @@
|
||||
class Word < ApplicationRecord
|
||||
has_many :definitions
|
||||
|
||||
def definitions_for(language)
|
||||
self.definitions.where(language_id: language)
|
||||
end
|
||||
end
|
||||
|
@@ -1,12 +1,12 @@
|
||||
<table>
|
||||
<tr>
|
||||
<td><b>Part of Speech</b></td>
|
||||
<td><b>Definition</b></td>
|
||||
<td class="px-2"><b>Part of Speech</b></td>
|
||||
<td class="px-2"><b>Definition</b></td>
|
||||
</tr>
|
||||
<% word.definitions.each do |definition| %>
|
||||
<% word.definitions_for(language).each do |definition| %>
|
||||
<tr>
|
||||
<td><b><%= definition.pos %></b></td>
|
||||
<td><%= definition.definition %></td>
|
||||
<td class="px-2"><b><%= definition.pos %></b></td>
|
||||
<td class="px-2"><%= definition.definition %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
@@ -2,13 +2,13 @@
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td><b>Part of Speech</b></td>
|
||||
<td><b>Definition</b></td>
|
||||
<td class="px-2"><b>Part of Speech</b></td>
|
||||
<td class="px-2"><b>Definition</b></td>
|
||||
</tr>
|
||||
<% parts_of_speech.each do |item| %>
|
||||
<tr>
|
||||
<td><b><%= item.pos %></b></td>
|
||||
<td><%= item.definition %></td>
|
||||
<td class="px-2"><b><%= item.pos %></b></td>
|
||||
<td class="px-2"><%= item.definition %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
@@ -7,6 +7,6 @@
|
||||
<% @words.each do |word| %>
|
||||
<div class="dictionary-entry p-4">
|
||||
<h2><%= word.word %></h2>
|
||||
<%= render "definitions", word: word %>
|
||||
<%= render "definitions", word: word, language: @language %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
@@ -23,25 +23,9 @@
|
||||
<div class="d-flex mb-3">
|
||||
<div class="p-2">
|
||||
<%= link_to "Dictionary", dictionary_index_path %>
|
||||
<% if user_signed_in? %>
|
||||
<pre style="display: inline;">|</pre>
|
||||
<%= link_to "Languages", admin_languages_path %>
|
||||
<pre style="display: inline;">|</pre>
|
||||
<%= form_tag '/set_active_language', class: "d-inline", id: "active_language_form" do %>
|
||||
<%= active_language_select_tag %>
|
||||
<%= submit_tag "Switch Language", id: "active_language_submit_tag" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
<div class="ms-auto p-2">
|
||||
<% if user_signed_in? %>
|
||||
<p style="display: inline;">Welcome <%= current_user.username %></p>
|
||||
<pre style="display: inline;">|</pre>
|
||||
<%= link_to "Sign Out", destroy_user_session_path, data: { turbo_method: :delete } %>
|
||||
<% else %>
|
||||
<%= link_to "Sign In", new_user_session_path %>
|
||||
<pre style="display: inline;">|</pre>
|
||||
<%= link_to "Register", new_user_registration_path %>
|
||||
<%= form_tag '/set_active_language', class: 'd-inline', id: 'active_language_form' do %>
|
||||
<%= active_language_select_tag %>
|
||||
<%= submit_tag "Switch Language", id: "active_language_submit_tag" %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user